简体   繁体   中英

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.

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.

There is no official documented format of the binary serialized data. The closest I came across was http://primates.ximian.com/~lluis/dist/binary_serialization_format.htm . So there is little possibility of getting a third party Python package which would do this for you. Even if it does, its likely to break in the future.

If you want to stick with Binary Serialization your best bet is to use IronPython and rely on the CLR to serialize the data.

Else, for interoperability beyond CLR, use either SOAP or XML Serialization.

Like stated above you can use pythonnet and clr. its not really Python anymore but it should get the stuff you want...

import clr
import System
#requires pythonnet installed -> pip install pythonnet 
clr.AddReference("YourDLLAssemblyName") # usually requires dll to be within directory

from System.Runtime.Serialization.Formatters.Binary import BinaryFormatter
from System.IO import FileStream,FileMode,FileAccess,FileShare

filepath = '<PathToYourDataFile>'
serializer = BinaryFormatter()
reader = FileStream(filepath, FileMode.Open, FileAccess.Read, FileShare.None)
data = serializer.Deserialize(reader)
print(data, data.GetType())
#from here on you can propably go on via reflection or try to cast it
reader.Close()

No. It can't be done the way you want to, in any reasonable time frame.

You need to deserialize it into XML, and then use something like this to turn it into python lists/maps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM