# Turning a byte array into a C# object whose type is unknown at compile time

I'm trying to turn an array of bytes into a C# object. I only the know the type of the object at runtime.

Right now, I'm using the JsonConvert.DeserializeObject<T> to do this, but the result is clunky, mostly because I don't know T at compile time.

If I knew T at compile time, I could simply do:

string json = System.Text.Encoding.UTF8.GetString(buffer);
return JsonConvert.DeserializeObject<MyType>(json);


But since I don't, I must do:

string json = System.Text.Encoding.UTF8.GetString(buffer);
var deserializeInfoMethodInfo = typeof(JsonConvert).GetMethods().Where(method => method.Name == "DeserializeObject" && method.IsGenericMethod).First();
return deserializeInfoMethodInfo.MakeGenericMethod(paramInfo.ParameterType).Invoke(null, new object[] { json });


Is there a way to make this simpler?

I have experimented with something like this:

using (MemoryStream stream = new MemoryStream(buffer, bufferIndex, paramLengths[i])) {
var serializer = new JsonSerializer();
}
}


This is arguably more readable, but I don't like how I have to get the buffer (which I got from calling stream.Read(buffer, 0, length) in another place), turn it back into a stream, and then have the serializer iterate through the stream again.

Is there a way to trim down the second snippet, or should I stop whining and use the third?

I think, it can be simplified just a little bit:

static object Deserialize(byte[] buffer, Type type)
{
{
}
}


NB: StreamReader releases/disposes MemoryStream, so you don't have to encapsulate that in a using()

Or You could even do:

static object Deserialize(byte[] buffer, Type type)
{
return JsonConvert.DeserializeObject(System.Text.Encoding.UTF8.GetString(buffer), type);
}


or:

static object Deserialize(byte[] buffer, Type type)
{
return JToken.Parse(Encoding.UTF8.GetString(buffer)).ToObject(type);
}

• No idea why that DeserializeObject overload slipped out of my mind. I'll use your second example. Thanks – pushkin Apr 21 '18 at 16:04
• @pushkin but you are aware of that with the second example you're reading the same data (at least) three times? 1) from source to buffer 2) from buffer to string 3) from string to parse --- as I wrote in my answer, you're streams are incorrectly chained... – t3chb0t Apr 21 '18 at 17:20
using (MemoryStream stream = new MemoryStream(buffer, bufferIndex, paramLengths[i])) {
var serializer = new JsonSerializer();
}
}


This is exactly as it should be done. Using reflection as if in your first example isn't optimal because if there is an easy way to avoid it (and there is) you should not make it more complicted.

You could however clean this up a bit.

using (var memoryStream = new MemoryStream(buffer, bufferIndex, paramLengths[i]))
{
var serializer = new JsonSerializer();

• usings don't have to be nested
• the streamReader needs to be disposed too
I don't like how I have to get the buffer (which I got from calling stream.Read(buffer, 0, length) in another place), turn it back into a stream, and then have the serializer iterate through the stream again.
• I believe the streamReader using is unnecessary, because JsonTextReader will take ownership of it and dispose of it when it's disposed: see here. I saw this mentioned in a couple of other places too – pushkin Apr 21 '18 at 15:50