Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why YAMLGenerator dont close stream depends on configuration

Tags:

java

yaml

jackson

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.

like image 418
hudi Avatar asked May 21 '18 08:05

hudi


1 Answers

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);
like image 164
Dharita Chokshi Avatar answered Nov 18 '22 04:11

Dharita Chokshi