Skip to content

Commit

Permalink
Fixed #140
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Jul 25, 2019
1 parent 49390c6 commit 456f36f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 4 deletions.
4 changes: 4 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ Modules:
=== Releases ===
------------------------------------------------------------------------

2.10.0.pr2 (not yet released)

#140: (yaml) Implement `JsonGenerator.writeFieldId(...)` for `YAMLGenerator`

2.10.0.pr1 (19-Jul-2019)

#50: (yaml) Empty string serialized without quotes if MINIMIZE_QUOTES is enabled
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,18 @@ public final void writeFieldName(SerializableString name)
_writeFieldName(name.getValue());
}

@Override // override since 2.10 (method added in 2.8)
public void writeFieldId(long id) throws IOException {
// 24-Jul-2019, tatu: Should not force construction of a String here...
String idStr = Long.valueOf(id).toString(); // since instances for small values cached
if (_writeContext.writeFieldName(idStr) == JsonWriteContext.STATUS_EXPECT_VALUE) {
_reportError("Can not write a field id, expecting a value");
}
// to avoid quoting
// _writeFieldName(idStr);
_writeScalar(idStr, "int", STYLE_SCALAR);
}

@Override
public final void writeStringField(String fieldName, String value)
throws IOException
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package com.fasterxml.jackson.dataformat.yaml.ser;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.*;

public class GeneratorWithMinimizeTest extends ModuleTestBase
{
private final static YAMLMapper MINIM_MAPPER = new YAMLMapper();
static {
MINIM_MAPPER.enable(YAMLGenerator.Feature.MINIMIZE_QUOTES);
}
private final ObjectMapper VANILLA_MAPPER = newObjectMapper();

private final YAMLMapper MINIM_MAPPER = YAMLMapper.builder()
.enable(YAMLGenerator.Feature.MINIMIZE_QUOTES)
.build();

public void testDefaultSetting() {
YAMLFactory f = new YAMLFactory();
Expand Down Expand Up @@ -165,4 +168,31 @@ public void testEmptyStringWithMinimizeQuotes() throws Exception

assertEquals("---\nkey: \"\"", yaml);
}

// [dataformats-text#140]
public void testNumberKey() throws Exception
{
// First, test with Strings that happen to look like Integer
final Map<String, String> stringKeyMap = Collections.singletonMap(
"42", "answer");
// Quoted in both cases
assertEquals("---\n\"42\": \"answer\"",
VANILLA_MAPPER.writeValueAsString(stringKeyMap).trim());
// but not if minimizing quotes
assertEquals("---\n\"42\": answer",
MINIM_MAPPER.writeValueAsString(stringKeyMap).trim());

// And then true Integer keys

final Map<Integer, String> intKeyMap = Collections.singletonMap(
Integer.valueOf(42), "answer");

// by default, is quoted
assertEquals("---\n42: \"answer\"",
VANILLA_MAPPER.writeValueAsString(intKeyMap).trim());

// but not if minimizing quotes
assertEquals("---\n42: answer",
MINIM_MAPPER.writeValueAsString(intKeyMap).trim());
}
}

0 comments on commit 456f36f

Please sign in to comment.