Skip to content

Commit 1331fc2

Browse files
dswieckicowtowncoder
authored andcommitted
Issue #175 - implement array with indicator indentation flag
1 parent d4531e3 commit 1331fc2

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

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

+13
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,15 @@ public enum Feature implements FormatFeature // since 2.9
116116
* @since 2.9
117117
*/
118118
INDENT_ARRAYS(false),
119+
/**
120+
* Feature enabling of which adds indentation with indicator for array entry generation
121+
* (default indentation being 2 spaces).
122+
*<p>
123+
* Default value is `false` for backwards compatibility
124+
*
125+
* @since 2.12.0
126+
*/
127+
INDENT_ARRAYS_WITH_INDICATOR(false),
119128

120129
/**
121130
* Option passed to SnakeYAML that determines if the line breaks used for
@@ -291,6 +300,10 @@ protected DumperOptions buildDumperOptions(int jsonFeatures, int yamlFeatures,
291300
opt.setIndicatorIndent(1);
292301
opt.setIndent(2);
293302
}
303+
if (Feature.INDENT_ARRAYS_WITH_INDICATOR.enabledIn(_formatFeatures)) {
304+
opt.setIndicatorIndent(2);
305+
opt.setIndentWithIndicator(true);
306+
}
294307
// 14-May-2018: [dataformats-text#84] allow use of platform linefeed
295308
if (Feature.USE_PLATFORM_LINE_BREAKS.enabledIn(_formatFeatures)) {
296309
opt.setLineBreak(DumperOptions.LineBreak.getPlatformLineBreak());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.fasterxml.jackson.dataformat.yaml.ser;
2+
3+
import com.fasterxml.jackson.dataformat.yaml.ModuleTestBase;
4+
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator;
5+
import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;
6+
7+
import java.util.Map;
8+
9+
public class GeneratorFeature175Test extends ModuleTestBase {
10+
/*
11+
/**********************************************************
12+
/* Test methods
13+
/**********************************************************
14+
*/
15+
16+
// [dataformats-text#175]: arrays indentation with indicator
17+
public void testArrayWithIndicatorIndentation() throws Exception {
18+
String yamlBefore = "---\n" +
19+
"tags:\n" +
20+
" - tag:\n" +
21+
" values:\n" +
22+
" - \"first\"\n" +
23+
" - \"second\"\n" +
24+
" name: \"Mathematics\"";
25+
26+
YAMLMapper defaultArrayMapper = YAMLMapper.builder()
27+
.enable(YAMLGenerator.Feature.INDENT_ARRAYS)
28+
.build();
29+
Map<?, ?> stuff = defaultArrayMapper.readValue(yamlBefore, Map.class);
30+
String defaultYaml = defaultArrayMapper.writeValueAsString(stuff);
31+
32+
//default array indentation set indicator in separate line
33+
assertNotSame(yamlBefore, defaultYaml);
34+
35+
YAMLMapper arrayWithIndicatorMapper = YAMLMapper.builder()
36+
.enable(YAMLGenerator.Feature.INDENT_ARRAYS)
37+
.enable(YAMLGenerator.Feature.INDENT_ARRAYS_WITH_INDICATOR)
38+
.build();
39+
40+
String arrayWithIndicatorYaml = arrayWithIndicatorMapper.writeValueAsString(stuff);
41+
assertEquals(yamlBefore, arrayWithIndicatorYaml.trim());
42+
43+
// and do it again to ensure it is parseable (no need to be identical)
44+
Map<?, ?> stuff2 = arrayWithIndicatorMapper.readValue(arrayWithIndicatorYaml, Map.class);
45+
assertNotNull(stuff2);
46+
assertEquals(stuff, stuff2);
47+
}
48+
}

0 commit comments

Comments
 (0)