0

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?

Mohamoud Mohamed
  • 515
  • 7
  • 16
  • 1
    Serialized to *string*, using Base64 encoding. –  Nov 16 '17 at 18:08
  • 1
    Serialization only means "turning an in-memory representation of objects into a string of characters". There is no single standard of doing things. .Net alone offers multiple ways of doing it, and BinaryFormatter is just one of them. – Guillaume CR Nov 16 '17 at 18:11
  • 1
    `����ÿÿÿÿ������� ���JMessageBoardBackend, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null���%MessageBoardBackend.Controllers.MyImg���k__BackingFieldk__BackingFieldk__BackingField������� ������hiiiiiii�������` Interesting content, there. –  Nov 16 '17 at 18:17
  • https://en.wikipedia.org/wiki/Base64 – pm100 Nov 16 '17 at 18:21
  • Is there a way to see the source of the whole process end to end? – Mohamoud Mohamed Nov 16 '17 at 18:33
  • Will how did you do that? – Mohamoud Mohamed Nov 16 '17 at 18:35
  • 1
    .Net Core is open source, so you could technically look at every line of code and see how it happens. The other version of .Net is not, so you would need to use Reflection to get an idea of what's going on. – Guillaume CR Nov 16 '17 at 18:37
  • 1
    Here's the source code of [BinaryFormatter](https://github.com/dotnet/corefx/blob/master/src/System.Runtime.Serialization.Formatters/src/System/Runtime/Serialization/Formatters/Binary/BinaryFormatter.cs) – Guillaume CR Nov 16 '17 at 18:39
  • Thanks for the help guys beginning to understand whats going on here. – Mohamoud Mohamed Nov 16 '17 at 18:56

1 Answers1

2

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.)

nvoigt
  • 75,013
  • 26
  • 93
  • 142
  • Thank you for this.I will add validation to exclude those. – Mohamoud Mohamed Nov 16 '17 at 18:57
  • @fasecity You probably don't want to roll your own serialization. Like most cases in software engineering, reinventing the wheel is the last option. – Guillaume CR Nov 16 '17 at 19:14
  • No that wasnt the case at all. The thing is I want to understand how it works because im just sick of looking at code like its magic and just plug it in my code base without understanding it. I love abstraction but this is probably one of the best discoveries in human history and I just wanted to see how they did it. – Mohamoud Mohamed Nov 16 '17 at 19:23