Skip to content

Commit

Permalink
Fix #53
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Nov 30, 2017
1 parent d95b5c3 commit 3bb1792
Show file tree
Hide file tree
Showing 15 changed files with 222 additions and 146 deletions.
6 changes: 4 additions & 2 deletions release-notes/VERSION
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ Modules:

2.9.3 (not yet released)

#39 (yamk): Binary data not recognized by YAML parser
(repoted by tmoschou@github)
#42 (csv): Add support for escaping double quotes with the configured escape character
(contributed by frankgrimes97@github)
#51 (csv): Set of custom objects with `IGNORE_UNKNOWN` brokes silently csv
(reported by Simone L)
#39 (yamk): Binary data not recognized by YAML parser
(repoted by tmoschou@github)
#53: (yaml) Binary values written without type tag
(reported by arulrajnet@github)

2.9.2 (14-Oct-2017)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import org.yaml.snakeyaml.DumperOptions.FlowStyle;
import org.yaml.snakeyaml.emitter.Emitter;
import org.yaml.snakeyaml.events.*;
import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder;
import org.yaml.snakeyaml.nodes.Tag;

import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.core.base.GeneratorBase;
Expand Down Expand Up @@ -155,6 +157,7 @@ private Feature(boolean defaultState) {
protected final static long MIN_INT_AS_LONG = (long) Integer.MIN_VALUE;
protected final static long MAX_INT_AS_LONG = (long) Integer.MAX_VALUE;
protected final static Pattern PLAIN_NUMBER_P = Pattern.compile("[0-9]*(\\.[0-9]*)?");
protected final static String TAG_BINARY = Tag.BINARY.toString();

/*
/**********************************************************
Expand Down Expand Up @@ -186,7 +189,8 @@ private Feature(boolean defaultState) {
private final static Character STYLE_LITERAL = Character.valueOf('|');

// Which flow style to use for Base64? Maybe basic quoted?
private final static Character STYLE_BASE64 = Character.valueOf('"');
// 29-Nov-2017, tatu: Actually SnakeYAML uses block style so:
private final static Character STYLE_BASE64 = STYLE_LITERAL;

private final static Character STYLE_PLAIN = null;

Expand Down Expand Up @@ -616,12 +620,10 @@ public void writeBinary(Base64Variant b64variant, byte[] data, int offset, int l
return;
}
_verifyValueWrite("write Binary value");
// ok, better just Base64 encode as a String...
if (offset > 0 || (offset+len) != data.length) {
data = Arrays.copyOfRange(data, offset, offset+len);
}
String encoded = b64variant.encode(data);
_writeScalar(encoded, "byte[]", STYLE_BASE64);
_writeScalarBinary(data);
}

/*
Expand Down Expand Up @@ -785,13 +787,24 @@ protected void _releaseBuffers() {
*/

// Implicit means that (type) tags won't be shown, right?
private final static ImplicitTuple DEFAULT_IMPLICIT = new ImplicitTuple(true, true);
private final static ImplicitTuple NO_TAGS = new ImplicitTuple(true, true);

// ... and sometimes we specifically DO want explicit tag:
private final static ImplicitTuple EXPLICIT_TAGS = new ImplicitTuple(false, false);

protected void _writeScalar(String value, String type, Character style) throws IOException
{
_emitter.emit(_scalarEvent(value, style));
}

private void _writeScalarBinary(byte[] data) throws IOException
{
// 29-Nov-2017, tatu: Use SnakeYAML encoder instead of Jackson's
String encoded = Base64Coder.encodeLines(data);
_emitter.emit(new ScalarEvent(null, TAG_BINARY, EXPLICIT_TAGS, encoded,
null, null, STYLE_BASE64));
}

protected ScalarEvent _scalarEvent(String value, Character style)
{
String yamlTag = _typeId;
Expand All @@ -802,7 +815,9 @@ protected ScalarEvent _scalarEvent(String value, Character style)
if (anchor != null) {
_objectId = null;
}
return new ScalarEvent(anchor, yamlTag, DEFAULT_IMPLICIT, value,
// 29-Nov-2017, tatu: Not 100% sure why we don't force explicit tags for
// type id, but trying to do so seems to double up tag output...
return new ScalarEvent(anchor, yamlTag, NO_TAGS, value,
null, null, style);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ public void testNativeSerialization() throws Exception
assertEquals("--- !<impl>\na: 13", yaml);
}

// [Issue#22]
public void testNonNativeSerialization() throws Exception
{
YAMLMapper mapper = new YAMLMapper();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.fasterxml.jackson.dataformat.yaml;
package com.fasterxml.jackson.dataformat.yaml.deser;

import java.util.ArrayList;
import java.util.List;

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

public class DatabindAdvancedTest extends ModuleTestBase
{
Expand Down Expand Up @@ -135,7 +136,7 @@ public Image(String uri, String title, int w, int h, Size s)
/**********************************************************
*/

public void testBasic() throws Exception
public void testReadComplexPojo() throws Exception
{
ObjectMapper mapper = newObjectMapper();
String YAML =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.fasterxml.jackson.dataformat.yaml;
package com.fasterxml.jackson.dataformat.yaml.deser;

import java.io.ByteArrayInputStream;
import java.util.Map;
Expand All @@ -8,12 +8,13 @@

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.dataformat.yaml.ModuleTestBase;

/**
* Unit tests for checking functioning of the databinding
* on top of YAML layer.
*/
public class SimpleDatabindTest extends ModuleTestBase
public class DatabindReadTest extends ModuleTestBase
{
static class EmptyBean {
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.fasterxml.jackson.dataformat.yaml;
package com.fasterxml.jackson.dataformat.yaml.deser;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.dataformat.yaml.ModuleTestBase;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.fasterxml.jackson.dataformat.yaml.YAMLParser;

import java.io.StringWriter;
import java.math.BigInteger;
Expand All @@ -14,6 +17,47 @@ public class SimpleParseTest extends ModuleTestBase
{
final YAMLFactory YAML_F = new YAMLFactory();

public void testBasic() throws Exception
{
final String YAML =
"string: 'text'\n"
+"bool: true\n"
+"bool2: false\n"
+"null: null\n"
+"i: 123\n"
+"d: 1.25\n"
;
JsonParser p = YAML_F.createParser(YAML);
assertToken(JsonToken.START_OBJECT, p.nextToken());

assertToken(JsonToken.FIELD_NAME, p.nextToken());
assertToken(JsonToken.VALUE_STRING, p.nextToken());
assertEquals("text", p.getText());
assertToken(JsonToken.FIELD_NAME, p.nextToken());
assertToken(JsonToken.VALUE_TRUE, p.nextToken());
assertEquals("true", p.getText());
assertToken(JsonToken.FIELD_NAME, p.nextToken());
assertToken(JsonToken.VALUE_FALSE, p.nextToken());
assertEquals("false", p.getText());
assertToken(JsonToken.FIELD_NAME, p.nextToken());
assertToken(JsonToken.VALUE_NULL, p.nextToken());
assertEquals("null", p.getText());
assertToken(JsonToken.FIELD_NAME, p.nextToken());
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
assertEquals("123", p.getText());
assertEquals(123, p.getIntValue());
assertToken(JsonToken.FIELD_NAME, p.nextToken());
assertToken(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
assertEquals("1.25", p.getText());
assertEquals(1.25, p.getDoubleValue());
assertEquals(1, p.getIntValue());

assertToken(JsonToken.END_OBJECT, p.nextToken());
assertNull(p.nextToken());
assertNull(p.nextToken());
assertNull(p.nextToken());
p.close();
}
// Parsing large numbers around the transition from int->long and long->BigInteger
public void testIntParsing() throws Exception
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.fasterxml.jackson.dataformat.yaml;
package com.fasterxml.jackson.dataformat.yaml.deser;

import org.junit.Test;

import com.fasterxml.jackson.dataformat.yaml.UTF8Reader;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.fasterxml.jackson.dataformat.yaml.ser;

import org.junit.Assert;

import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.node.JsonNodeType;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.dataformat.yaml.ModuleTestBase;

public class BinaryWriteTest extends ModuleTestBase
{
private final ObjectMapper MAPPER = newObjectMapper();

public void testBinaryViaTree() throws Exception
{
byte[] srcPayload = new byte[] { 1, 2, 3, 4, 5 };
ObjectNode root = MAPPER.createObjectNode();
root.put("payload", srcPayload);
String doc = MAPPER.writeValueAsString(root);

// and read back
final JsonNode bean = MAPPER.readTree(doc);
final JsonNode data = bean.get("payload");
assertNotNull(data);
assertEquals(JsonNodeType.BINARY, data.getNodeType());
final byte[] b = data.binaryValue();
Assert.assertArrayEquals(srcPayload, b);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.fasterxml.jackson.dataformat.yaml.ser;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeSet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.dataformat.yaml.ModuleTestBase;

public class DatabindWriteTest extends ModuleTestBase
{
public void testBasicPOJO() throws Exception
{
ObjectMapper mapper = newObjectMapper();
FiveMinuteUser user = new FiveMinuteUser("Bob", "Dabolito", false,
FiveMinuteUser.Gender.MALE, new byte[] { 1, 3, 13, 79 });
String yaml = mapper.writeValueAsString(user).trim();
String[] parts = yaml.split("\n");
// unify ordering, need to use TreeSets to get alphabetic ordering
TreeSet<String> exp = new TreeSet<String>();
for (String part : parts) {
exp.add(part.trim());
}
Iterator<String> it = exp.iterator();
assertEquals("---", it.next());
assertEquals("AQMNTw==", it.next());
assertEquals("firstName: \"Bob\"", it.next());
assertEquals("gender: \"MALE\"", it.next());
assertEquals("lastName: \"Dabolito\"", it.next());
assertEquals("userImage: !!binary |", it.next());
assertEquals("verified: false", it.next());
assertFalse(it.hasNext());
}

public void testWithFile() throws Exception
{
File f = File.createTempFile("test", ".yml");
f.deleteOnExit();
ObjectMapper mapper = newObjectMapper();
Map<String,Integer> map = new HashMap<String,Integer>();
map.put("a", 3);
mapper.writeValue(f, map);
assertTrue(f.canRead());
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(
f), "UTF-8"));
String doc = br.readLine();
String str = br.readLine();
if (str != null) {
doc += "\n" + str;
}
doc = trimDocMarker(doc);
assertEquals("a: 3", doc);
br.close();
f.delete();
}

public void testWithFile2() throws Exception
{
File f = File.createTempFile("test", ".yml");
f.deleteOnExit();
ObjectMapper mapper = newObjectMapper();
ObjectNode root = mapper.createObjectNode();
root.put("name", "Foobar");
mapper.writeValue(f, root);

// and get it back
Map<?,?> result = mapper.readValue(f, Map.class);
assertEquals(1, result.size());
assertEquals("Foobar", result.get("name"));
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package com.fasterxml.jackson.dataformat.yaml;
package com.fasterxml.jackson.dataformat.yaml.ser;

import java.util.Map;

import com.fasterxml.jackson.dataformat.yaml.ModuleTestBase;
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator;
import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator.Feature;

public class GeneratorFeature34Test extends ModuleTestBase
{
/*
Expand Down
Loading

0 comments on commit 3bb1792

Please sign in to comment.