Byte array serialization in JSON.NET
- by Daniel Earwicker
Given this simple class:
class HasBytes
{
public byte[] Bytes { get; set; }
}
I can round-trip it through JSON using JSON.NET such that the byte array is base-64 encoded:
var bytes = new HasBytes { Bytes = new byte[] { 1, 2, 3, 4 } };
// turn it into a JSON string
var json = JsonConvert.SerializeObject(bytes);
// get back a new instance…