Skip to content
This repository was archived by the owner on Dec 19, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

public class YAMLGenerator extends GeneratorBase
{


/**
* Enumeration that defines all togglable features for YAML generators
*/
Expand Down Expand Up @@ -66,7 +68,18 @@ public enum Feature // implements FormatFeature // for 2.7
*
* @since 2.6
*/
SPLIT_LINES(true)
SPLIT_LINES(true),

/**
* Whether strings will be rendered without quotes (true) or
* with quotes (false, default).
*<p>
* Minimized quote usage makes for more human readable output; however, content 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>.
* @since 2.6
*/
MINIMIZE_QUOTES(false)

;

Expand Down Expand Up @@ -126,11 +139,15 @@ private Feature(boolean defaultState) {
// numbers, booleans, should use implicit
private final static Character STYLE_SCALAR = null;
// Strings quoted for fun
private final static Character STYLE_STRING = Character.valueOf('"');

private final static Character STYLE_QUOTED = Character.valueOf('"');
// Strings in literal (block) style
private final static Character STYLE_LITERAL = Character.valueOf('|');

// Which flow style to use for Base64? Maybe basic quoted?
private final static Character STYLE_BASE64 = Character.valueOf('"');

private final static Character STYLE_PLAIN = null;

/*
/**********************************************************
/* Output state
Expand Down Expand Up @@ -439,7 +456,16 @@ public void writeString(String text) throws IOException,JsonGenerationException
return;
}
_verifyValueWrite("write String value");
_writeScalar(text, "string", STYLE_STRING);
Character style = STYLE_QUOTED;
if (Feature.MINIMIZE_QUOTES.enabledIn(_formatFeatures)) {
if (text.contains("\n")) {
style = STYLE_LITERAL;
}
else {
style = STYLE_PLAIN;
}
}
_writeScalar(text, "string", style);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,42 @@ public void testSplitLines() throws Exception
yaml);
}

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

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

YAMLMapper mapper = new YAMLMapper(f);

Map<String, Object> content = new HashMap<String, Object>();
content.put("key", "some value");
String yaml = mapper.writeValueAsString(content).trim();

assertEquals("---\n" +
"key: some value", yaml);
}

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

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

YAMLMapper mapper = new YAMLMapper(f);

Map<String, Object> content = new HashMap<String, Object>();
content.put("key", "first\nsecond\nthird");
String yaml = mapper.writeValueAsString(content).trim();

assertEquals("---\n" +
"key: |-\n first\n second\n third", yaml);
}

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