1

I have this class object:

public class Gallery
{
    public int Id { get; set; }
    public bool IsUploaded { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
    public byte[] Image;
    public string JobRef { get; set; }
}

I create an instance of it and serialize it using Newton JSON:

var json = JsonConvert.SerializeObject('the object');

I then convert the while of this to a byte array and send it my socket listening on another PC:

byte[] byteArray = Encoding.UTF8.GetBytes(json);

using (NetworkStream serverStream = clientSocket.GetStream())
{
    serverStream.Write(byteArray, 0, byteArray.Length);
   //more code here but not relevant as the error happens on listening PC
}

On my listening PC I receive the json as so:

using (IInputStream input = args.Socket.InputStream)
{
    byte[] data = new byte[BufferSize];
    IBuffer buffer = data.AsBuffer();
    uint dataRead = BufferSize;
    while (dataRead == BufferSize)
    {
        await input.ReadAsync(buffer, BufferSize, InputStreamOptions.Partial);
        request.Append(Encoding.UTF8.GetString(data, 0, data.Length));
        dataRead = buffer.Length;
    }
}

var job = JsonConvert.DeserializeObject<Gallery>(request.ToString());

On this last line I get the error:

Additional text encountered after finished reading JSON content: r. Path '', line 34, position 2.

Yet, if I remove the byte array image there is no error. So, is there a certain way to include image arrays with a json object?

Thanks

Andrew Simpson
  • 6,883
  • 11
  • 79
  • 179

1 Answers1

4

Json does not support byte array. You need to convert it to Base64. See this thread, and also this one.

Community
  • 1
  • 1
Always Learning
  • 312
  • 1
  • 10