Skip to content

Exception when decoding Jackson-encoded Base64 binary value in YAML #90

@tlrx

Description

@tlrx

Using jackson-dataformats-text-2.9.6 binary values that exceed a given length cannot be decoded back and the following exception is thrown:

com.fasterxml.jackson.core.JsonParseException: Illegal character '\' (code 0x5c) in base64 content

This is related to #62 which forces the Base64Variant to be MIME. When the encoded value exceeds a given size, line seperators are inserted into the encoded value but it then fails to be decoded.

I created the following small test and added it to the SimpleGenerationTest in yaml:

public void testCase() throws Exception {
        final int length = 50;
        final byte[] data = new byte[length];
        Arrays.fill(data, (byte) 1);

        final YAMLFactory yamlFactory = new YAMLFactory();

        final ByteArrayOutputStream os = new ByteArrayOutputStream();
        try (JsonGenerator generator = yamlFactory.createGenerator(os, JsonEncoding.UTF8)) {
            generator.writeStartObject();
            generator.writeBinaryField("data", data);
            generator.writeEndObject();
        }

        try (JsonParser parser = yamlFactory.createParser(os.toByteArray())) {
            assertEquals(JsonToken.START_OBJECT, parser.nextToken());
            assertEquals(JsonToken.FIELD_NAME, parser.nextToken());
            assertEquals("data", parser.currentName());
            assertEquals(JsonToken.VALUE_EMBEDDED_OBJECT, parser.nextToken());
            assertArrayEquals(data, parser.getBinaryValue());
            assertEquals(JsonToken.END_OBJECT, parser.nextToken());
            assertNull(parser.nextToken());
        }
    }

This test passed correctly when length is small (50) but with larger size (like 100) it fails.

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