Search code examples
.netarraysjsonbytestream

Why do byte arrays turn to gibberish when serialized?


What happens to the original structure of the data that was passed through a call like this:

 MyImg bytes = new MyImg
            {
                Id = 1,
                Img = new byte[] { 1, 0, 5 },
                Text = "hiiiiiii"
            };

        IFormatter formatter2 = new BinaryFormatter();
        using (MemoryStream stream = new MemoryStream())
        {
            formatter2.Serialize(stream, bytes);
            bytes.Img = stream.ToArray();
        }

There is a lot of abstraction to how this produces:

{"id":1,"img":"AAEAAAD/////AQAAAAAAAAAMAgAAAEpNZXNzYWdlQm9hcmRCYWNrZW5kLCBWZXJzaW9uPTEuMC4wLjAsIEN1bHR1cmU9bmV1dHJhbCwgUHVibGljS2V5VG9rZW49bnVsbAUBAAAAJU1lc3NhZ2VCb2FyZEJhY2tlbmQuQ29udHJvbGxlcnMuTXlJbWcDAAAAEzxJZD5rX19CYWNraW5nRmllbGQUPEltZz5rX19CYWNraW5nRmllbGQVPFRleHQ+a19fQmFja2luZ0ZpZWxkAAcBCAICAAAAAQAAAAkDAAAABgQAAAAIaGlpaWlpaWkPAwAAAAMAAAACAQAFCw==","text":"hiiiiiii"}

Im just trying to understand whats going on.

Is serial/deserialization a universal process or does .Net do something different then Java?


Solution

  • Your byte array is encoded as Base64 string in JSON, that's a common way to make sure your serialized bytes don't contain characters that aren't printable or used by the serializers themselves (for example < or > in XML, { or } in JSON etc.)