4

I'm wondering if an object serialized with the .NET Framework BinaryFormatter can be deserialized with the .NET Core BinaryFormatter and vice versa.

To what extent are these formats compatible?

Context: I'm porting parts of a legacy app to .NET Core and this code used the BinaryFormatter to do .NET Remoting which is not supported anymore. So I wonder how to replace the serialization of objects. First trials have shown that sometimes the BinaryFormatter can be used (which would be great because then I have less code changes), in other situations deserialization failed with seemingly random error messages such as "no root object" or "type needs to implement IConvertible" which seem to indicate that the format are not compatible.

In this SO question, I'm trying to understand to what extent the formats are compatible too see if there could be easy work-arounds instead of switching to another serialization technique/framework altogether.

Dejan
  • 9,150
  • 8
  • 69
  • 117
  • That's easy to test. Serialize the same objects using both formatters and compare the binaries. Are they the same? How do they differ? Is it just a few bytes with the runtime version or are there more significant differences? *What* do you serialize? If the classes have changed, their serialized forms will be different too. Errors like `Type needs to implement IConvertible` refer to your program's types, not the format. – Panagiotis Kanavos Mar 07 '18 at 12:35
  • 1
    If the formats are the same, it may be a bug in BinaryFormatter [like this one](https://github.com/dotnet/corefx/issues/21918). People in that thread serialized the binaries to Base64 in order to compare them. – Panagiotis Kanavos Mar 07 '18 at 12:36
  • What happens if you put the code that uses BinaryFormatter in a .NET Standard library and share it with both frameworks? – Panagiotis Kanavos Mar 07 '18 at 12:36
  • As I said, I already have a couple of cases that work and some that don't. I was reaching out to SO to maybe find someone who already knows the answer instead of reverse-engineering the BinaryFormatter. Also, errors like the the IConvertible might be due to a random state of the deserializer because the format has changed. My apps a sharing the exact same code, so classes haven't changed of course. – Dejan Mar 07 '18 at 12:39
  • SO isn't the CoreFX team or learn.microsoft.com. If you have examples post them in the question itself. How can anyone help when you don't provide anything? – Panagiotis Kanavos Mar 07 '18 at 12:42
  • @PanagiotisKanavos: but the GitHub thread link you've sent is very interesting. It seems to strongly indicate that the intention is that both formatter produce the same output. This is giving me hope. – Dejan Mar 07 '18 at 12:42
  • and the issue says they *should* be the same. So, post an example here. Right now, you don't even mention *which* version you used. Is it a 2.0 Preview? 2.0 RTM? If you open an issue on the CoreFX site you'll be asked to post an actual example too – Panagiotis Kanavos Mar 07 '18 at 12:44
  • @PanagiotisKanavos: thx for your input. I'll try to make minimal repros and post it there and then I'll update this SO question. The reason I haven't done this yet is because I was assuming that the formats wouldn't be compatible. But it's great if they are. – Dejan Mar 07 '18 at 12:48

1 Answers1

0

In my case, .net core does not recognize List (I mean the list will be null) in .net framework and vice versa. I am considering using Newtonsoft.Json.

Usually, when you create an ASP.NET MVC project, Newtonsoft.Json is installed automatically.

I Serialized the following in .net core:

SubtitlesAnalysis ana = GetAnalysis();
string output = Newtonsoft.Json.JsonConvert.SerializeObject(input);
File.WriteAllText(@"D:\1.json", output);

and I Deserialzed it in .net framework:

var input = File.ReadAllText("D:\1.json");       
var ana = Newtonsoft.Json.JsonConvert.DeserializeObject<SubtitlesAnalysis>(input);

Class SubtitlesAnalysis :

    [Serializable()]
    public class SubtitlesAnalysis
    {
        public int WordCountOfBasic
        {
            get
            {
                return WordCountsByDifficulty[1];
            }
        }

        public int WordCountOfCet4
        {
            get
            {
                return WordCountsByDifficulty[2];
            }
        }

        public int WordCountOfCet6
        {
            get
            {
                return WordCountsByDifficulty[3];
            }
        }

        public int WordCountOfIeltsOrTofel
        {
            get
            {
                return WordCountsByDifficulty[4];
            }
        }

        public int WordCountOfGre
        {
            get
            {
                return WordCountsByDifficulty[5];
            }
        }

        public int WordCountOfGrePlus
        {
            get
            {
                return WordCountsByDifficulty[6];
            }
        }

        public int WordCount
        {
            get
            {
                return WordCountsByDifficulty[1] + WordCountsByDifficulty[2] + WordCountsByDifficulty[3] + WordCountsByDifficulty[4] + WordCountsByDifficulty[5] + WordCountsByDifficulty[6];
            }
        }

        private int[] _WordCountsByDifficulty = new int[8];
        public int[] WordCountsByDifficulty
        {
            get
            {
                return _WordCountsByDifficulty;
            }
        }

        public int IdiomCount { get; set; }
        public int Speed { get; set; }
        public TimeSpan Length { get; set; }
        public TimeSpan DialogueTime { get; set; }
        public List<WordIdWithContext> WordAndContext { get; set; } = new List<WordIdWithContext>();
        public List<WordIdWithContext> IdiomAndContext { get; set; } = new List<WordIdWithContext>();
        public List<WordWithContext> UnrecognisedWordsWithContext { get; set; } = new List<WordWithContext>();

        public SubtitlesAnalysis()
        {
        }
    }
Eric Chow
  • 411
  • 4
  • 7