-1

I am serializing an object to a stream to store as file and then retrieving and trying to deserialize the object, but get an error parsing. Below is code:

var content = JsonConvert.SerializeObject(data);
var output = new MemoryStream();
var writer = new StreamWriter(output, Encoding.UTF8);
writer.Write(content);
writer.Flush();
//write to some file...


//when reading the file
Stream filestream;
//filestream opens some file stream
byte[] buffer = new byte[4096]
using(MemoryStream ms = new MemoryStream()){
int read;
while((read = filestream.Read(buffer, 0, buffer.Length)) > 0){
ms.Write(buffer, 0, read);
}
var data = Encoding.UTF8.GetString(ms.ToArray());
//encounters error here. I can see that first few chars of the string are question marks.
JsonConvert.DeserializeObject<T>(data);
Riz
  • 6,486
  • 19
  • 66
  • 106
  • My quickest guess is that your buffer is too small and the content is larger than 4096 bytes so you are not reading the whole thing back. – NavidM Nov 22 '21 at 20:48
  • Side points: I hope this is pseudo-code because you need `using` blocks. You can use `CopyTo` instead of looping – Charlieface Nov 23 '21 at 00:54

1 Answers1

0

A couple ideas. Are you getting an exception?

Try encapsulating your write & reads into functions and wrap the memoryStream and streamWriter in using statements as well

using var writer = new StreamWriter(...

Additionally you don't need to set the length of the buffer to 4096, that's probably messing up your encoding. Read your memorystream to an array like so

var buffer = ms.ToArray();

How can I write MemoryStream to byte[]

It looks like you're having an issue copying the memory between the 2 memory streams though. Ideally you would write and read to a file or some other source rather than between 2 memory stream objects but you can copy the contents directly using a copyto

https://learn.microsoft.com/en-us/dotnet/api/system.io.stream.copyto?view=net-6.0

Edit: Adding pseudocode

Assuming your content has been read into Stream filestream;

using var MemoryStream ms = new MemoryStream();
filestream.CopyTo(ms);
var data = Encoding.UTF8.GetString(ms.ToArray());
return JsonConvert.DeserializeObject<T>(data);
Zakk Diaz
  • 1,063
  • 10
  • 15
  • yes i'm first putting it into S3 and then later reading it. I combined the two to keep it brief and only posted relevant parts. – Riz Nov 22 '21 at 20:55
  • You're making it too complicated. You don't need to do any loops, just copy your Stream filestream to a MemoryStream like in the link and call ToArray on the memorystream to get the bytes. – Zakk Diaz Nov 23 '21 at 22:16