Skip to content

Commit 456f36f

Browse files
committed
Fixed #140
1 parent 49390c6 commit 456f36f

File tree

3 files changed

+50
-4
lines changed

3 files changed

+50
-4
lines changed

release-notes/VERSION-2.x

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ Modules:
88
=== Releases ===
99
------------------------------------------------------------------------
1010

11+
2.10.0.pr2 (not yet released)
12+
13+
#140: (yaml) Implement `JsonGenerator.writeFieldId(...)` for `YAMLGenerator`
14+
1115
2.10.0.pr1 (19-Jul-2019)
1216

1317
#50: (yaml) Empty string serialized without quotes if MINIMIZE_QUOTES is enabled

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

+12
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,18 @@ public final void writeFieldName(SerializableString name)
443443
_writeFieldName(name.getValue());
444444
}
445445

446+
@Override // override since 2.10 (method added in 2.8)
447+
public void writeFieldId(long id) throws IOException {
448+
// 24-Jul-2019, tatu: Should not force construction of a String here...
449+
String idStr = Long.valueOf(id).toString(); // since instances for small values cached
450+
if (_writeContext.writeFieldName(idStr) == JsonWriteContext.STATUS_EXPECT_VALUE) {
451+
_reportError("Can not write a field id, expecting a value");
452+
}
453+
// to avoid quoting
454+
// _writeFieldName(idStr);
455+
_writeScalar(idStr, "int", STYLE_SCALAR);
456+
}
457+
446458
@Override
447459
public final void writeStringField(String fieldName, String value)
448460
throws IOException

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

+34-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
package com.fasterxml.jackson.dataformat.yaml.ser;
22

3+
import java.util.Collections;
34
import java.util.HashMap;
45
import java.util.Map;
56

7+
import com.fasterxml.jackson.databind.ObjectMapper;
68
import com.fasterxml.jackson.dataformat.yaml.*;
79

810
public class GeneratorWithMinimizeTest extends ModuleTestBase
911
{
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();
1417

1518
public void testDefaultSetting() {
1619
YAMLFactory f = new YAMLFactory();
@@ -165,4 +168,31 @@ public void testEmptyStringWithMinimizeQuotes() throws Exception
165168

166169
assertEquals("---\nkey: \"\"", yaml);
167170
}
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+
}
168198
}

0 commit comments

Comments
 (0)