Skip to content

Commit 32bdc37

Browse files
ShauniArimaFriou Ander
andauthored
Allow to use long keys in YAML (#334)
Co-authored-by: Friou Ander <[email protected]>
1 parent ad815fc commit 32bdc37

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,16 @@ public enum Feature implements FormatFeature // since 2.9
137137
* @since 2.9.6
138138
*/
139139
USE_PLATFORM_LINE_BREAKS(false),
140+
141+
/**
142+
* Option passed to SnakeYAML to enable usage of key longer that 128 characters.
143+
* If disabled, the max key length is set to 128 characters.
144+
* <p>
145+
* Default value is {@code false}.
146+
*
147+
* @since 2.14
148+
*/
149+
USE_LONG_KEYS(false),
140150
;
141151

142152
protected final boolean _defaultState;
@@ -310,6 +320,10 @@ protected DumperOptions buildDumperOptions(int jsonFeatures, int yamlFeatures,
310320
if (Feature.USE_PLATFORM_LINE_BREAKS.enabledIn(_formatFeatures)) {
311321
opt.setLineBreak(DumperOptions.LineBreak.getPlatformLineBreak());
312322
}
323+
324+
if (Feature.USE_LONG_KEYS.enabledIn(_formatFeatures)) {
325+
opt.setMaxSimpleKeyLength(1024);
326+
}
313327
return opt;
314328
}
315329

yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/ser/GeneratorFeatureTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,21 @@ public void testArrayIndentation() throws Exception
6464
assertEquals("- \"third\"", parts[3].trim());
6565
}
6666

67+
//@since 2.14
68+
public void testLongKeys() throws Exception
69+
{
70+
final String LONG_KEY = "key_longer_than_128_characters_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
71+
72+
ObjectMapper defaultMapper = YAMLMapper.builder().build();
73+
final Object inputValue = Collections.singletonMap(LONG_KEY, "value");
74+
assertEquals("---\n? " + LONG_KEY + "\n: \"value\"",
75+
_trim(defaultMapper.writeValueAsString(inputValue)));
76+
77+
ObjectMapper longKeysMapper = YAMLMapper.builder().enable(YAMLGenerator.Feature.USE_LONG_KEYS).build();
78+
assertEquals("---\n" + LONG_KEY + ": \"value\"",
79+
_trim(longKeysMapper.writeValueAsString(inputValue)));
80+
}
81+
6782
// @since 2.12
6883
public void testYAMLSpecVersionDefault() throws Exception
6984
{

0 commit comments

Comments
 (0)