Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to read C# serialized objects into Python?

I have a binary file containing C# serialized objects.

I can read the contents with python, but get results similar to:

'T\x00\x00\x00Test.Jobs.GenerateJobRequest, POC.Server\xca\x02-\xa2\x02\t\x82\x01\x06\x1a\x04myahR\x1d\x08\xfe\xff\xff\xff\xff\xff\xff\xff\xff\x01\x12\x10Data Lite Exportp\t\n\x16Do_Ko_Change-Job__ID_23\x10\x0c\x18\xa7\xb9\x18(\x012\x00:\x00H\xbc\x08')

Is there a way to deserialize this object in python?

I agree this is not an optimal solution and JSON, XML would be better. However, I do not have control of the process that serializes the data, I am only the consumer.

like image 657
Noah Avatar asked Oct 06 '22 09:10

Noah


1 Answers

From your question, it is unclear which version of Python (CPython, Jython, IronPython) you are using. But I assume you are using CPython, as for IronPython it would be trivial.

There is a library for CPython, Python .NET. It serves as a binding between .NET and Python, and works really nice. Even generics are supported. Though it seems not to be actively supported anymore, I've been using it for a while now. It works like a charm.

You'll need Visual Studio to compile it, but probably it will work with Visual Studio Express (though I do not know).

With this, you can import any .NET-dll. Assuming you can deserialize it in C#, you then should be able to deserialize it in Python as well.

like image 191
Thorsten Kranz Avatar answered Oct 10 '22 01:10

Thorsten Kranz