Skip to content

Binary data not recognized by YAML parser #39

@tmoschou

Description

@tmoschou

Fails to recognise explicit binary type tag !!binary (tag:yaml.org,2002:binary)

Also fails to decode to spec as defined in http://yaml.org/type/binary.html.

Binary data is serialized using the base64 format as defined by RFC2045 (MIME), with the following notes:

  • The content is not restricted to lines of 76 characters or less.

  • Characters other than the base64 alphabet, line breaks and white space are considered an error.

Sample test case the demonstrates the issues:

From "Example 2.23. Various Explicit Tags" from spec (1.1 and 1.2) - http://www.yaml.org/spec/1.2/spec.html

---
picture: !!binary |
 R0lGODlhDAAMAIQAAP//9/X
 17unp5WZmZgAAAOfn515eXv
 Pz7Y6OjuDg4J+fn5OTk6enp
 56enmleECcgggoBADs=
@Test
public void testBinary() throws IOException {
    final ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
    try (final InputStream inputStream = getClass().getResourceAsStream("yaml1.3-example2.23.yaml")) {
        final JsonNode bean = mapper.readTree(inputStream);
        final JsonNode picture = bean.get("picture");
        assertEquals(JsonNodeType.BINARY, picture.getNodeType()); // fails
        final byte[] gif = picture.binaryValue(); // also fails 
        assertEquals(65, gif.length);
        final byte[] actualFileHeader = Arrays.copyOfRange(gif, 0, 6);
        final byte[] expectedFileHeader = new byte[]{'G', 'I', 'F', '8', '9', 'a'};
        assertArrayEquals(expectedFileHeader, actualFileHeader);
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    yamlIssue related to YAML format backend

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions