Skip to content

Commit d95b5c3

Browse files
committed
Fix #39
1 parent 99be568 commit d95b5c3

File tree

14 files changed

+78
-35
lines changed

14 files changed

+78
-35
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<parent>
44
<groupId>com.fasterxml.jackson</groupId>
55
<artifactId>jackson-base</artifactId>
6-
<version>2.9.2</version>
6+
<version>2.9.3-SNAPSHOT</version>
77
</parent>
88
<groupId>com.fasterxml.jackson.dataformat</groupId>
99
<artifactId>jackson-dataformats-text</artifactId>

release-notes/VERSION

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ Modules:
1414
(contributed by frankgrimes97@github)
1515
#51 (csv): Set of custom objects with `IGNORE_UNKNOWN` brokes silently csv
1616
(reported by Simone L)
17+
#39 (yamk): Binary data not recognized by YAML parser
18+
(repoted by tmoschou@github)
1719

1820
2.9.2 (14-Oct-2017)
1921

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

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,17 @@
77

88
import org.yaml.snakeyaml.error.Mark;
99
import org.yaml.snakeyaml.events.*;
10+
import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder;
1011
import org.yaml.snakeyaml.nodes.NodeId;
1112
import org.yaml.snakeyaml.nodes.Tag;
1213
import org.yaml.snakeyaml.parser.ParserImpl;
1314
import org.yaml.snakeyaml.reader.StreamReader;
15+
import org.yaml.snakeyaml.resolver.Resolver;
1416

1517
import com.fasterxml.jackson.core.*;
1618
import com.fasterxml.jackson.core.base.ParserBase;
1719
import com.fasterxml.jackson.core.io.IOContext;
1820
import com.fasterxml.jackson.core.util.BufferRecycler;
19-
import com.fasterxml.jackson.core.util.ByteArrayBuilder;
20-
21-
import org.yaml.snakeyaml.resolver.Resolver;
2221

2322
/**
2423
* {@link JsonParser} implementation used to expose YAML documents
@@ -441,7 +440,7 @@ public JsonToken nextToken() throws IOException
441440
}
442441
}
443442

444-
protected JsonToken _decodeScalar(ScalarEvent scalar)
443+
protected JsonToken _decodeScalar(ScalarEvent scalar) throws IOException
445444
{
446445
String value = scalar.getValue();
447446
_textValue = value;
@@ -451,7 +450,6 @@ protected JsonToken _decodeScalar(ScalarEvent scalar)
451450

452451
if (typeTag == null || typeTag.equals("!")) { // no, implicit
453452
Tag nodeTag = _yamlResolver.resolve(NodeId.scalar, value, scalar.getImplicit().canOmitTagInPlainScalar());
454-
455453
if (nodeTag == Tag.STR) {
456454
return JsonToken.VALUE_STRING;
457455
}
@@ -480,6 +478,14 @@ protected JsonToken _decodeScalar(ScalarEvent scalar)
480478
typeTag = typeTag.split(",")[0];
481479
}
482480
}
481+
// [dataformats-text#39]: support binary type
482+
if ("binary".equals(typeTag)) {
483+
// 29-Nov-2017, tatu: two choices for base64 codecs (or 3 with Java 8):
484+
// Jackson's own or SnakeYAML. Former is faster, but maybe makes sense
485+
// to use latter for sake of consistency.
486+
_binaryValue = Base64Coder.decodeLines(value);
487+
return JsonToken.VALUE_EMBEDDED_OBJECT;
488+
}
483489
// canonical values by YAML are actually 'y' and 'n'; but plenty more unofficial:
484490
if ("bool".equals(typeTag)) { // must be "true" or "false"
485491
Boolean B = _matchYAMLBoolean(value, len);
@@ -645,23 +651,21 @@ public int getText(Writer writer) throws IOException
645651

646652
@Override
647653
public Object getEmbeddedObject() throws IOException {
654+
if (_currToken == JsonToken.VALUE_EMBEDDED_OBJECT ) {
655+
return _binaryValue;
656+
}
648657
return null;
649658
}
650659

651-
// TODO: can remove from 2.9 or so (base impl added in 2.8)
652-
@SuppressWarnings("resource")
660+
// Base impl from `ParserBase` works fine here:
661+
// public byte[] getBinaryValue(Base64Variant variant) throws IOException
662+
653663
@Override
654-
public byte[] getBinaryValue(Base64Variant variant) throws IOException
664+
public int readBinaryValue(Base64Variant b64variant, OutputStream out) throws IOException
655665
{
656-
if (_binaryValue == null) {
657-
if (_currToken != JsonToken.VALUE_STRING) {
658-
_reportError("Current token ("+_currToken+") not VALUE_STRING, can not access as binary");
659-
}
660-
ByteArrayBuilder builder = _getByteArrayBuilder();
661-
_decodeBase64(getText(), builder, variant);
662-
_binaryValue = builder.toByteArray();
663-
}
664-
return _binaryValue;
666+
byte[] b = getBinaryValue(b64variant);
667+
out.write(b);
668+
return b.length;
665669
}
666670

667671
/*

yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/DatabindAdvancedTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ public Image(String uri, String title, int w, int h, Size s)
137137

138138
public void testBasic() throws Exception
139139
{
140-
ObjectMapper mapper = mapperForYAML();
140+
ObjectMapper mapper = newObjectMapper();
141141
String YAML =
142142
"---\n"
143143
+"content:\n"

yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/ExceptionConversionTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public class ExceptionConversionTest extends ModuleTestBase
1010
{
1111
public void testSimpleParsingLeakage() throws Exception
1212
{
13-
YAMLMapper mapper = mapperForYAML();
13+
YAMLMapper mapper = newObjectMapper();
1414
try {
1515
mapper.readTree("foo:\nbar: true\n baz: false");
1616
fail("Should not pass with invalid YAML");

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public Words(String... w) {
2121
/**********************************************************
2222
*/
2323

24-
private final ObjectMapper MAPPER = mapperForYAML();
24+
private final ObjectMapper MAPPER = newObjectMapper();
2525

2626
public void testArrayIndentation() throws Exception
2727
{
@@ -39,7 +39,7 @@ public void testArrayIndentation() throws Exception
3939
// 14-Mar-2017, tatu: Note that we can not, alas, dynamically change
4040
// features via ObjectWriter yet as they are bound at YAMLGenerator
4141
// construction time.
42-
final YAMLMapper indentingMapper = mapperForYAML();
42+
final YAMLMapper indentingMapper = newObjectMapper();
4343
indentingMapper.getFactory().enable(YAMLGenerator.Feature.INDENT_ARRAYS);
4444

4545
yaml = indentingMapper.writeValueAsString(input);

yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/ModuleTestBase.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ protected ModuleTestBase() { }
6767
/* Helper methods, setup
6868
/**********************************************************************
6969
*/
70-
71-
protected YAMLMapper mapperForYAML()
70+
71+
protected YAMLMapper newObjectMapper()
7272
{
7373
return new YAMLMapper();
7474
}

yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/ObjectIdTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public ObjectIdGenerator.IdKey key(Object key) {
109109
+" next: 1"
110110
;
111111

112-
private final ObjectMapper MAPPER = mapperForYAML();
112+
private final ObjectMapper MAPPER = newObjectMapper();
113113

114114
public void testNativeSerialization() throws Exception
115115
{

yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/SimpleDatabindTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ static class Name {
3333
/**********************************************************
3434
*/
3535

36-
private final ObjectMapper MAPPER = mapperForYAML();
36+
private final ObjectMapper MAPPER = newObjectMapper();
3737

3838
public void testSimpleNested() throws Exception
3939
{

yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/SimpleGenerationTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public void testStreamingNested() throws Exception
7575

7676
public void testBasicPOJO() throws Exception
7777
{
78-
ObjectMapper mapper = mapperForYAML();
78+
ObjectMapper mapper = newObjectMapper();
7979
FiveMinuteUser user = new FiveMinuteUser("Bob", "Dabolito", false,
8080
FiveMinuteUser.Gender.MALE, new byte[] { 1, 3, 13, 79 });
8181
String yaml = mapper.writeValueAsString(user).trim();
@@ -105,7 +105,7 @@ public void testWithFile() throws Exception
105105
{
106106
File f = File.createTempFile("test", ".yml");
107107
f.deleteOnExit();
108-
ObjectMapper mapper = mapperForYAML();
108+
ObjectMapper mapper = newObjectMapper();
109109
Map<String,Integer> map = new HashMap<String,Integer>();
110110
map.put("a", 3);
111111
mapper.writeValue(f, map);
@@ -127,7 +127,7 @@ public void testWithFile2() throws Exception
127127
{
128128
File f = File.createTempFile("test", ".yml");
129129
f.deleteOnExit();
130-
ObjectMapper mapper = mapperForYAML();
130+
ObjectMapper mapper = newObjectMapper();
131131
ObjectNode root = mapper.createObjectNode();
132132
root.put("name", "Foobar");
133133
mapper.writeValue(f, root);

0 commit comments

Comments
 (0)