Search code examples
c#pythonbinarydeserialization

Deserialize a Byte Array created in C# in Python


I have a 2D jagged double array in C# which I convert to a byte array like this:

byte[][][] byteArray = new byte[10][][];

I am saving the byte array as a binary file in this way:

BinaryFormatter formatter = new BinaryFormatter();
using (FileStream stream = new FileStream(path, FileMode.Create, FileAccess.Write))
{
   formatter.Serialize(stream, byteArray);
}

Now, I need to read the file in python in order to re-build there the 2D double array...

I was trying to use numpy.fromfile() and would like to know how this should be done.


Solution

  • From what I can tell, BinaryFormatter and numpy.fromfile() are not made to cross platforms, let alone languages. It will be easier to use a format that is more cross-platform, like JSON. The portion of converting the double to a byte[] might also have issues, e.g. because of endianness. If the performance and data requirements aren't really an issue, it'd be easier to not complicate things.

    This example uses Json.NET for the C#:

    double[][] myArray = // whatever
    
    var path = // whatever
    using (StreamWriter file = File.CreateText(path))
    {
        JsonSerializer serializer = new JsonSerializer();
        serializer.Serialize(file, myArray);
    }
    

    And then in the Python all you have to do is something like:

    import numpy
    import json
    path = # whatever
    with open(path) as f:
        myArray = numpy.array(json.load(f))
    # we now have the array! e.g.
    print(myArray.dtype) # float64
    print(myArray[0][0]) # 0.79449418131