Your question is similar to: Jackson JSON custom serialization for certain fields
What is the desired behavior? Do you want the stream to be serialized as a string, as binary content, parsed and serialized as an object, etc.
Assuming you want the data serialized as a string, you could use an annotation to tell Jackson how to serialize it:
public class TextFile {
String fileName;
@JsonSerialize(using=ReaderToStringSerializer.class, as=String.class)
StringReader content;
}
public class ReaderToStringSerializer extends JsonSerializer<Reader> {
@Override
public void serialize(Reader contents,
JsonGenerator jsonGenerator,
SerializerProvider serializerProvider)
throws IOException, JsonProcessingException {
StringBuilder buffer = new StringBuilder();
char[] array = new char[8096];
int len = 0;
while (-1 != (len = contents.read(array))) {
buffer.append(array,0,len);
}
contents.close();
jsonGenerator.writeString(buffer.toString());
}
The output would be something like:
{
"fileName": "foo.bar",
"content": "the quick brown fox..."
}