I have some python data, serialized to pickles and need to use it in C# program. So is there any way to deserialize python pickles in C#? I can't change data format to JSON or etc.
5 Answers
You say you can't change the program that generates the pickle. But surely you can write a separate Python program to read the pickle and write it out 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)
1 Comment
Quote from the documentation:
The data format used by pickle is Python-specific. This has the advantage that there are no restrictions imposed by external standards such as XDR (which can’t represent pointer sharing); however it means that non-Python programs may not be able to reconstruct pickled Python objects.
So the answer to your question is no, you cannot deserialize it in C#. You will have to use an interoperable format such as XML or JSON if you need to communicate with other platforms.
Comments
You can try embedding IronPython and unpickling from there, then making the unpickled 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 have access to all modules defining the classes of all objects contained in the pickle (same as in CPython).
You should try to serialize your data some other more interoperable way (such as JSON or XML) if possible.
1 Comment
There is now a NuGet Razorvine.Pickle, for serializing and deserializing pickle files in .NET.