Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(yaml) Binary values written without type tag #53

Closed
arulrajnet opened this issue Nov 12, 2017 · 1 comment
Closed

(yaml) Binary values written without type tag #53

arulrajnet opened this issue Nov 12, 2017 · 1 comment
Milestone

Comments

@arulrajnet
Copy link

arulrajnet commented Nov 12, 2017

YAML binary data http://yaml.org/type/binary.html

This code

  public void yamlBinaryTest() throws IOException {
    StringWriter out = new StringWriter();
    YAMLFactory f = new YAMLFactory();
    f.disable(Feature.WRITE_DOC_START_MARKER);
    f.enable(Feature.MINIMIZE_QUOTES);
    YAMLGenerator gen = f.createGenerator(out);
    gen.writeStartObject();
    gen.writeFieldName("files");
    gen.writeStartArray();
    for(int i=0; i<=2; i++) {
      gen.writeStartObject();
      gen.writeStringField("path", "/tmp/"+i+".txt");
      byte[] data = "foobar".getBytes("UTF-8");
      gen.writeFieldName("content");
      gen.writeBinary(Base64Variants.MIME, data, 0, data.length);
      gen.writeEndObject();
    }
    gen.writeEndArray();
    gen.writeEndObject();
    gen.close();
    System.out.println(out);
  }

Returns

files:
- path: /tmp/0.txt
  content: "Zm9vYmFy"
- path: /tmp/1.txt
  content: "Zm9vYmFy"
- path: /tmp/2.txt
  content: "Zm9vYmFy"

The expected result

files:
- path: /tmp/0.txt
  content: !!binary | 
    Zm9vYmFy
- path: /tmp/1.txt
  content: !!binary | 
    Zm9vYmFy
- path: /tmp/2.txt
  content: !!binary | 
    Zm9vYmFy

The YAMLGenerator does not support for binary literal.
SnakeYAML(actual low-level decoder/encoder) is having this feature https://bitbucket.org/asomov/snakeyaml/src/190f378138123971777405fbabeab704724b1860/src/main/java/org/yaml/snakeyaml/representer/SafeRepresenter.java?at=default&fileviewer=file-view-default#SafeRepresenter.java-430

@arulrajnet arulrajnet changed the title Not writes binary data as expected (yaml) Not writes binary data as expected Nov 12, 2017
@cowtowncoder
Copy link
Member

cowtowncoder commented Nov 13, 2017

Thank you for reporting this.

Seems related to #39 but for serialization (not deserialization).

I think it is reasonable that we would try to add metadata as suggested.
As to whether encoding should be done by snakeyaml emitter or jackson is an open question.

The only concern I have is that if serialization format changes there may older code that could have problems: however, since type id is just metadata and value itself does not change maybe that is fine.

@cowtowncoder cowtowncoder added this to the 2.9.3 milestone Nov 30, 2017
@cowtowncoder cowtowncoder changed the title (yaml) Not writes binary data as expected (yaml) Binary values written without type tag Nov 30, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants