Skip to content

Commit 10f2dc1

Browse files
committed
Fix #50
1 parent 08afb62 commit 10f2dc1

File tree

7 files changed

+34
-44
lines changed

7 files changed

+34
-44
lines changed

csv/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ abstractions.
4040
<dependency>
4141
<groupId>com.google.guava</groupId>
4242
<artifactId>guava</artifactId>
43-
<version>16.0.1</version>
43+
<version>18.0</version>
4444
<scope>test</scope>
4545
</dependency>
4646
</dependencies>

properties/pom.xml

+1-5
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,7 @@ configuration files as if there was implied nesting structure (by default using
2727
<scope>provided</scope>
2828
</dependency>
2929

30-
<dependency>
31-
<groupId>com.fasterxml.jackson.core</groupId>
32-
<artifactId>jackson-annotations</artifactId>
33-
<scope>test</scope>
34-
</dependency>
30+
<!-- and for testing need annotations; but should be available via `jackson-databind` above -->
3531
</dependencies>
3632

3733
<build>

release-notes/VERSION-2.x

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ Modules:
1010

1111
2.10.0 (not yet released)
1212

13+
#50: (yaml) Empty string serialized without quotes if MINIMIZE_QUOTES is enabled
14+
(reported by tim-palmer@github)
1315
#100: (properties) Add an option to specify properties prefix
1416
(contributed by Alon B-L)
1517
#101: (yaml) Use latest SnakeYAML version 1.24 and get rid of deprecated methods

yaml/pom.xml

+2-6
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,8 @@
3333
<version>1.24</version>
3434
</dependency>
3535

36-
<!-- and for testing need annotations -->
37-
<dependency>
38-
<groupId>com.fasterxml.jackson.core</groupId>
39-
<artifactId>jackson-annotations</artifactId>
40-
<scope>test</scope>
41-
</dependency>
36+
<!-- and for testing need annotations; but should be available via `jackson-databind` above
37+
-->
4238
</dependencies>
4339

4440
<build>

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

+18-6
Original file line numberDiff line numberDiff line change
@@ -588,25 +588,37 @@ public void writeString(String text) throws IOException,JsonGenerationException
588588
return;
589589
}
590590
_verifyValueWrite("write String value");
591-
DumperOptions.ScalarStyle style = STYLE_QUOTED;
592-
if (Feature.MINIMIZE_QUOTES.enabledIn(_formatFeatures) && !isBooleanContent(text)) {
591+
DumperOptions.ScalarStyle style;
592+
593+
// [dataformats-text#50]: Empty String always quoted
594+
if (text.isEmpty()) {
595+
style = STYLE_QUOTED;
596+
} else if (Feature.MINIMIZE_QUOTES.enabledIn(_formatFeatures)) {
597+
if (isBooleanContent(text)) {
598+
style = STYLE_QUOTED;
593599
// If this string could be interpreted as a number, it must be quoted.
594-
if (Feature.ALWAYS_QUOTE_NUMBERS_AS_STRINGS.enabledIn(_formatFeatures)
600+
} else if (Feature.ALWAYS_QUOTE_NUMBERS_AS_STRINGS.enabledIn(_formatFeatures)
595601
&& PLAIN_NUMBER_P.matcher(text).matches()) {
596602
style = STYLE_QUOTED;
597603
} else if (text.indexOf('\n') >= 0) {
598604
style = STYLE_LITERAL;
599605
} else {
600606
style = STYLE_PLAIN;
601607
}
602-
} else if (Feature.LITERAL_BLOCK_STYLE.enabledIn(_formatFeatures) && text.indexOf('\n') >= 0) {
603-
style = STYLE_LITERAL;
608+
_writeScalar(text, "string", style);
609+
return;
610+
} else {
611+
if (Feature.LITERAL_BLOCK_STYLE.enabledIn(_formatFeatures) && text.indexOf('\n') >= 0) {
612+
style = STYLE_LITERAL;
613+
} else {
614+
style = STYLE_QUOTED;
615+
}
604616
}
605617
_writeScalar(text, "string", style);
606618
}
607619

608620
private boolean isBooleanContent(String text) {
609-
return text.equals("true") || text.equals("false");
621+
return "true".equals(text) || "false".equals(text);
610622
}
611623

612624
@Override

yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/failing/GeneratorWithMinimize50Test.java

-26
This file was deleted.

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

+10
Original file line numberDiff line numberDiff line change
@@ -127,4 +127,14 @@ public void testNonQuoteNumberStoredAsString() throws Exception
127127
assertEquals("---\n" +
128128
"key: 2.0.1.2.3", yaml);
129129
}
130+
131+
// [dataformats-test#50]
132+
public void testEmptyStringWithMinimizeQuotes() throws Exception
133+
{
134+
Map<String, Object> content = new HashMap<>();
135+
content.put("key", "");
136+
String yaml = MINIM_MAPPER.writeValueAsString(content).trim();
137+
138+
assertEquals("---\nkey: \"\"", yaml);
139+
}
130140
}

0 commit comments

Comments
 (0)