|
1 | 1 | package com.fasterxml.jackson.dataformat.yaml.ser;
|
2 | 2 |
|
| 3 | +import java.util.Collections; |
3 | 4 | import java.util.HashMap;
|
4 | 5 | import java.util.Map;
|
5 | 6 |
|
| 7 | +import com.fasterxml.jackson.databind.ObjectMapper; |
6 | 8 | import com.fasterxml.jackson.dataformat.yaml.*;
|
7 | 9 |
|
8 | 10 | public class GeneratorWithMinimizeTest extends ModuleTestBase
|
9 | 11 | {
|
10 |
| - private final static YAMLMapper MINIM_MAPPER = new YAMLMapper(); |
11 |
| - static { |
12 |
| - MINIM_MAPPER.enable(YAMLGenerator.Feature.MINIMIZE_QUOTES); |
13 |
| - } |
| 12 | + private final ObjectMapper VANILLA_MAPPER = newObjectMapper(); |
| 13 | + |
| 14 | + private final YAMLMapper MINIM_MAPPER = YAMLMapper.builder() |
| 15 | + .enable(YAMLGenerator.Feature.MINIMIZE_QUOTES) |
| 16 | + .build(); |
14 | 17 |
|
15 | 18 | public void testDefaultSetting() {
|
16 | 19 | YAMLFactory f = new YAMLFactory();
|
@@ -165,4 +168,31 @@ public void testEmptyStringWithMinimizeQuotes() throws Exception
|
165 | 168 |
|
166 | 169 | assertEquals("---\nkey: \"\"", yaml);
|
167 | 170 | }
|
| 171 | + |
| 172 | + // [dataformats-text#140] |
| 173 | + public void testNumberKey() throws Exception |
| 174 | + { |
| 175 | + // First, test with Strings that happen to look like Integer |
| 176 | + final Map<String, String> stringKeyMap = Collections.singletonMap( |
| 177 | + "42", "answer"); |
| 178 | + // Quoted in both cases |
| 179 | + assertEquals("---\n\"42\": \"answer\"", |
| 180 | + VANILLA_MAPPER.writeValueAsString(stringKeyMap).trim()); |
| 181 | + // but not if minimizing quotes |
| 182 | + assertEquals("---\n\"42\": answer", |
| 183 | + MINIM_MAPPER.writeValueAsString(stringKeyMap).trim()); |
| 184 | + |
| 185 | + // And then true Integer keys |
| 186 | + |
| 187 | + final Map<Integer, String> intKeyMap = Collections.singletonMap( |
| 188 | + Integer.valueOf(42), "answer"); |
| 189 | + |
| 190 | + // by default, is quoted |
| 191 | + assertEquals("---\n42: \"answer\"", |
| 192 | + VANILLA_MAPPER.writeValueAsString(intKeyMap).trim()); |
| 193 | + |
| 194 | + // but not if minimizing quotes |
| 195 | + assertEquals("---\n42: answer", |
| 196 | + MINIM_MAPPER.writeValueAsString(intKeyMap).trim()); |
| 197 | + } |
168 | 198 | }
|
0 commit comments