Skip to content

JsonBinding#fromJson not closing InputStream after successfull completion #591

@Scharage

Description

@Scharage

Describe the bug
The problem is the following:
I want to use the fromJson method with a FileInputStream and noticed that the Stream is not closed as promised through the api documentation:

Description copied from interface:
jakarta.json.bind.Jsonb Reads in a JSON data from the specified InputStream and return the resulting content tree.
Specified by:
fromJson in interface Jsonb
Params:
stream – The stream is read as a JSON data. **Upon a successful completion, the stream will be closed by this method.**
clazz – Type of the content tree's root object.
Type parameters:
<T> – Type of the content tree's root object.
Returns:
the newly created root object of the java content tree
Throws:
JsonbException – If any unexpected error(s) occur(s) during deserialization.
NullPointerException – If any of the parameters is null.

To Reproduce
To reproduce the behaviour i wrote a code snippet. To make it functional replace PATHTOJSONFILE with an actual json file which holds {"key":"Test"} as a content for example.

import jakarta.json.bind.Jsonb;
import jakarta.json.bind.JsonbBuilder;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

public class Test {

	public static void main(String[] args) throws FileNotFoundException {
		Jsonb jsonb = JsonbBuilder.create();
		InputStream testStream = new FileInputStream(new File("PATHTOJSONFILE"));
		jsonb.fromJson(testStream,TestEntity.class);

		try{
			testStream.read();
			throw new RuntimeException("Failed, because stream was not closed.");
		}catch (IOException e) {
			//This is wanted as stream should be closed.
		}

	}

	public static class TestEntity{
		private String key;

		public TestEntity() {
		}
	}
}

Expected behavior
I expect the method to close the inputstream after completion, just as documented in the documentation. Or the documentation to be updated describing that its not closed, with a reason.

System information:

  • OS: Windows
  • Java Version: 11
  • Yasson Version: Noticed first 2.0.1 , tested in 3.0.2 again

Additional context
Add any other context about the problem here.
test.txt

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working right

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions