Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serialize/Deserialize a byte array in JSON.NET

I have a simple class with the following property:

[JsonObject(MemberSerialization.OptIn)] public class Person {     ...     [JsonProperty(PropertyName = "Photograph"]     public byte[] Photograph { get; set; }     ... } 

but this doesn't work when I populate the Photograph property with an image and transfer over http. This may sound like a simple question but I've yet to find a solution after looking online for hours, but, how do I serialise/deserialise a byte array in Json.NET? What attribute tags do I need, or, should I be doing this another way? Many thanks!

like image 790
Steve Randall Avatar asked Feb 18 '12 00:02

Steve Randall


1 Answers

public static T Deserialize<T>(byte[] data) where T : class {     using (var stream = new MemoryStream(data))     using (var reader = new StreamReader(stream, Encoding.UTF8))         return JsonSerializer.Create().Deserialize(reader, typeof(T)) as T; } 
like image 168
Alexey Zimarev Avatar answered Sep 19 '22 04:09

Alexey Zimarev