8

When creating objectMapper with yaml factory there are couple of configuration parameters which you can set:

   ObjectMapper o = new ObjectMapper(new YAMLFactory());
    // o.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false);
    // o.enable(SerializationFeature.CLOSE_CLOSEABLE);

problem is that this configuration is ignored in YAML Generator:

 @Override
    public void close() throws IOException
    {
        if (!isClosed()) {
            _emitter.emit(new DocumentEndEvent(null, null, false));
            _emitter.emit(new StreamEndEvent(null, null));
            super.close();
            _writer.close();
        }
    }

even when in javadoc is written something else

void com.fasterxml.jackson.dataformat.yaml.YAMLGenerator.close() throws IOException

Method called to close this generator, so that no more content can be written.

Whether the underlying target (stream, writer) gets closed depends on whether this generator either manages the target (i.e. is the only one with access to the target -- case if caller passes a reference to the resource such as File, but not stream); or has feature Feature.AUTO_CLOSE_TARGET enabled. If either of above is true, the target is also closed. Otherwise (not managing, feature not enabled), target is not closed.

Sergey
  • 448
  • 1
  • 4
  • 16
hudi
  • 15,555
  • 47
  • 142
  • 246
  • Calling code may want to write something after this YAML document. E.g. the output stream may be for writing HTTP servlet response. HTTP response may have some headers that go after the response payload. Maybe there are other use cases. However I would agree that there could be a better solution. – Petr Gladkikh May 23 '18 at 13:27
  • For what it's worth, the javadoc isn't written in the `YAMLGenerator` class but in jackson core's [`JsonGenerator` interface](https://github.com/FasterXML/jackson-core/blob/master/src/main/java/com/fasterxml/jackson/core/JsonGenerator.java#L1780) which `YAMLGenerator` implements by extending `GeneratorBase`. – Aaron May 23 '18 at 13:42
  • yes and when you extending method in JSON generator you should follow javadoc in method which you want to override. Or not ? – hudi May 24 '18 at 07:06
  • Sure, or at the very least write a javadoc that underlines the fact that you're not following it and why. But what I meant is that a derivation from a javadoc is more understandable (either as a mistake or design choice) when it's the javadoc of the interface of a class from another project than when it's your own. – Aaron May 28 '18 at 10:23
  • use this code `YAMLMapper o = new YAMLMapper(); o.configure(YAMLGenerator.Feature.WRITE_DOC_START_MARKER, true); o.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false); o.enable(SerializationFeature.CLOSE_CLOSEABLE);` – Amila kumara May 29 '18 at 18:52

1 Answers1

1

Use YAML Mapper instead of Object Mapper. This works fine for me.

YAMLMapper yamlMapper = new YAMLMapper(); 
yamlMapper.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false); 
yamlMapper.configure(YAMLGenerator.Feature.WRITE_DOC_START_MARKER, true);
yamlMapper.enable(SerializationFeature.CLOSE_CLOSEABLE);
Dharita Chokshi
  • 1,133
  • 3
  • 16
  • 39