Skip to content

Commit a66bee1

Browse files
committed
Merge branch '2.9'
2 parents bd61882 + d95b5c3 commit a66bee1

File tree

16 files changed

+80
-37
lines changed

16 files changed

+80
-37
lines changed

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
@@ -419,7 +418,7 @@ public JsonToken nextToken() throws IOException
419418
}
420419
}
421420

422-
protected JsonToken _decodeScalar(ScalarEvent scalar)
421+
protected JsonToken _decodeScalar(ScalarEvent scalar) throws IOException
423422
{
424423
String value = scalar.getValue();
425424
_textValue = value;
@@ -429,7 +428,6 @@ protected JsonToken _decodeScalar(ScalarEvent scalar)
429428

430429
if (typeTag == null || typeTag.equals("!")) { // no, implicit
431430
Tag nodeTag = _yamlResolver.resolve(NodeId.scalar, value, scalar.getImplicit().canOmitTagInPlainScalar());
432-
433431
if (nodeTag == Tag.STR) {
434432
return JsonToken.VALUE_STRING;
435433
}
@@ -458,6 +456,14 @@ protected JsonToken _decodeScalar(ScalarEvent scalar)
458456
typeTag = typeTag.split(",")[0];
459457
}
460458
}
459+
// [dataformats-text#39]: support binary type
460+
if ("binary".equals(typeTag)) {
461+
// 29-Nov-2017, tatu: two choices for base64 codecs (or 3 with Java 8):
462+
// Jackson's own or SnakeYAML. Former is faster, but maybe makes sense
463+
// to use latter for sake of consistency.
464+
_binaryValue = Base64Coder.decodeLines(value);
465+
return JsonToken.VALUE_EMBEDDED_OBJECT;
466+
}
461467
// canonical values by YAML are actually 'y' and 'n'; but plenty more unofficial:
462468
if ("bool".equals(typeTag)) { // must be "true" or "false"
463469
Boolean B = _matchYAMLBoolean(value, len);
@@ -623,23 +629,21 @@ public int getText(Writer writer) throws IOException
623629

624630
@Override
625631
public Object getEmbeddedObject() throws IOException {
632+
if (_currToken == JsonToken.VALUE_EMBEDDED_OBJECT ) {
633+
return _binaryValue;
634+
}
626635
return null;
627636
}
628637

629-
// TODO: can remove from 2.9 or so (base impl added in 2.8)
630-
@SuppressWarnings("resource")
638+
// Base impl from `ParserBase` works fine here:
639+
// public byte[] getBinaryValue(Base64Variant variant) throws IOException
640+
631641
@Override
632-
public byte[] getBinaryValue(Base64Variant variant) throws IOException
642+
public int readBinaryValue(Base64Variant b64variant, OutputStream out) throws IOException
633643
{
634-
if (_binaryValue == null) {
635-
if (_currToken != JsonToken.VALUE_STRING) {
636-
_reportError("Current token ("+_currToken+") not VALUE_STRING, can not access as binary");
637-
}
638-
ByteArrayBuilder builder = _getByteArrayBuilder();
639-
_decodeBase64(getText(), builder, variant);
640-
_binaryValue = builder.toByteArray();
641-
}
642-
return _binaryValue;
644+
byte[] b = getBinaryValue(b64variant);
645+
out.write(b);
646+
return b.length;
643647
}
644648

645649
/*

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/EventsTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public void testBasic() throws Exception
1717
+"i: 123\n"
1818
+"d: 1.25\n"
1919
;
20-
YAMLMapper mapper = mapperForYAML();
20+
YAMLMapper mapper = newObjectMapper();
2121
JsonParser p = mapper.createParser(YAML);
2222
assertToken(JsonToken.START_OBJECT, p.nextToken());
2323

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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public Words(String... w) {
2222
/**********************************************************
2323
*/
2424

25-
private final ObjectMapper MAPPER = mapperForYAML();
25+
private final ObjectMapper MAPPER = newObjectMapper();
2626

2727
public void testArrayIndentation() throws Exception
2828
{

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: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
public class SimpleGenerationTest extends ModuleTestBase
1212
{
13-
private final YAMLMapper MAPPER = mapperForYAML();
13+
private final YAMLMapper MAPPER = newObjectMapper();
1414

1515
public void testStreamingArray() throws Exception
1616
{
@@ -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)