jdereg / json-io

Convert Java to JSON. Convert JSON to Java. Pretty print JSON. Java JSON serializer. Deep copy Java object graphs.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

inputStream is closed by JsonReader.jsonToJava(inputStream)

raubv0gel opened this issue · comments

JsonReader.jsonToJava(inputStream, null); really should not close inputStream, but it currently does. In my case, there is more data after JSON in this stream: {"key": "value"} here comes more data.

See https://stackoverflow.com/questions/46953805/should-i-close-a-stream-which-i-did-not-create.

Workaround:

package com.innocon.nomicj.core;

import java.io.FilterInputStream;
import java.io.InputStream;


public class NonCloseableInputStream extends FilterInputStream {

	public NonCloseableInputStream(InputStream inputStream) {
		super(inputStream);
	}

	@Override
	public void close() {
		// don’t close!
	}
}

JsonReader gives you the option to do this either way. Using the static methods, it will close the streams. If you construct an instance of the JsonReader, and then use the readObject() methods, it will not close the stream. Take a look at the code inside the static methods to see how this is done.