Skip to content

Commit

Permalink
add literal scalar and folded scalar (#152)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr14huashao authored Jul 30, 2020
1 parent 1a8d5c5 commit 0924b0a
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/com/esotericsoftware/yamlbeans/YamlConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ public static enum WriteClassName {
}

public static enum Quote {
NONE('\0'), SINGLE('\''), DOUBLE('"');
NONE('\0'), SINGLE('\''), DOUBLE('"'), LITERAL('|'), FOLDED('>');

char c;

Expand Down
4 changes: 2 additions & 2 deletions src/com/esotericsoftware/yamlbeans/YamlWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ private void writeValue (Object object, Class fieldClass, Class elementType, Cla

if (fieldClass.isArray()) {
elementType = fieldClass.getComponentType();
emitter.emit(new SequenceStartEvent(anchor, null, true, false));
emitter.emit(new SequenceStartEvent(anchor, null, true, config.writeConfig.isFlowStyle()));
for (int i = 0, n = Array.getLength(object); i < n; i++)
writeValue(Array.get(object, i), elementType, null, null);
emitter.emit(Event.SEQUENCE_END);
Expand All @@ -267,7 +267,7 @@ private void writeValue (Object object, Class fieldClass, Class elementType, Cla
}

Set<Property> properties = Beans.getProperties(valueClass, config.beanProperties, config.privateFields, config);
emitter.emit(new MappingStartEvent(anchor, tag, !showTag, false));
emitter.emit(new MappingStartEvent(anchor, tag, !showTag, config.writeConfig.isFlowStyle()));
for (Property property : properties) {
try {
Object propertyValue = property.get(object);
Expand Down
20 changes: 13 additions & 7 deletions src/com/esotericsoftware/yamlbeans/emitter/Emitter.java
Original file line number Diff line number Diff line change
Expand Up @@ -525,14 +525,20 @@ private char chooseScalarStyle () {
ScalarEvent ev = (ScalarEvent)event;
if (analysis == null) analysis = ScalarAnalysis.analyze(ev.value, config.escapeUnicode);
if (ev.style == '"' || config.canonical) return '"';
if (ev.style == 0 && !(simpleKeyContext && (analysis.empty || analysis.multiline))
&& ((flowLevel != 0 && analysis.allowFlowPlain) || (flowLevel == 0 && analysis.allowBlockPlain))) return 0;
if (ev.style == 0 && ev.implicit[0] && !(simpleKeyContext && (analysis.empty || analysis.multiline))
&& ((flowLevel != 0 && analysis.allowFlowPlain) || (flowLevel == 0 && analysis.allowBlockPlain))) return 0;
if ((ev.style == '|' || ev.style == '>') && flowLevel == 0 && analysis.allowBlock) return '\'';
if ((ev.style == 0 || ev.style == '\'') && analysis.allowSingleQuoted && !(simpleKeyContext && analysis.multiline))
if ((ev.style == 0 || ev.style == '|' || ev.style == '>')
&& !(simpleKeyContext && (analysis.empty || analysis.multiline))
&& ((flowLevel != 0 && analysis.allowFlowPlain) || (flowLevel == 0 && analysis.allowBlockPlain))) {
return 0;
}
if ((ev.style == 0 || ev.style == '\'') && analysis.allowSingleQuoted
&& !(simpleKeyContext && analysis.multiline)) {
return '\'';
if (ev.style == 0 && analysis.multiline && flowLevel == 0 && analysis.allowBlock) return '|';
}
if ((ev.style == 0 || ev.style == '|' || ev.style == '>') && analysis.multiline && flowLevel == 0
&& analysis.allowBlock) {
return ev.style == 0 ? '|' : ev.style;
}

return '"';
}

Expand Down
18 changes: 18 additions & 0 deletions test/com/esotericsoftware/yamlbeans/YamlConfigTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,24 @@ public void testSetQuoteChar() throws YamlException {
yamlWriter.close();
assertEquals("\"name\": \"xxx\"" + LINE_SEPARATOR + "\"age\": \"18\"" + LINE_SEPARATOR,
stringWriter.toString());

stringWriter = new StringWriter();
yamlConfig.writeConfig.setQuoteChar(Quote.LITERAL);
yamlWriter = new YamlWriter(stringWriter, yamlConfig);
yamlWriter.write("test\ntest");
yamlWriter.close();
System.out.println(stringWriter.toString());
assertEquals("|-" + LINE_SEPARATOR + " test" + LINE_SEPARATOR + " test" + LINE_SEPARATOR,
stringWriter.toString());

stringWriter = new StringWriter();
yamlConfig.writeConfig.setQuoteChar(Quote.FOLDED);
yamlWriter = new YamlWriter(stringWriter, yamlConfig);
yamlWriter.write("test\ntest");
yamlWriter.close();
System.out.println(stringWriter.toString());
assertEquals(">-" + LINE_SEPARATOR + " test" + LINE_SEPARATOR + LINE_SEPARATOR + " test" + LINE_SEPARATOR,
stringWriter.toString());
}

@Test
Expand Down

0 comments on commit 0924b0a

Please sign in to comment.