How can I deserialize python pickles in C #?

I have some python data serialized to pickles and you need to use them in a C # program. So, is there a way to deserialize Pyrenean pickles in C #? I cannot change the data format in JSON, etc.

+7
source share
4 answers

You say you cannot change the program that creates the brine. But of course, can you write a separate Python program to read the brine and write it again as JSON?

import json, pickle with open("data.pickle", "rb") as fpick: with open("data.json", "w") as fjson: json.dump(pickle.load(fpick), fjson) 
+6
source

Quote from the documentation :

The data format used by the brine is specific to Python. This has the advantage that there are no restrictions imposed by external standards such as XDR (which cannot be a pointer exchange); however, this means that non-Python programs will not be able to recover pickled Python objects.

So, the answer to your question is no, you cannot deserialize it in C #. You will need to use a compatible format such as XML or JSON if you need to communicate with other platforms.

+4
source

You can try embedding IronPython and unzip from there, and then make the unclosed object available to the C # application.

Note that pickles are designed to serialize Python objects, so this approach only works if you have very simple objects with clear mappings to C # equivalents. It also requires that your IronPython environment has access to all modules that define the classes of all objects contained in pickle (same as CPython).

If possible, try serializing your data in another different way (such as JSON or XML).

+3
source

Pyrolite has an Unpickler class that turns a pickle into an object.

0
source

All Articles