I have some user-defined types where the object could not be saved by JLD or JLD2.
I’m wondering if there’s some way to “encode” an arbitrary object into a string, which I can then later “parse” (or “decode”) the string back to a equivalent object?
If you want a string output, just combine the previously mentioned serialization with base64 encoding.
julia> using Serialization, Base64
julia> test = base64encode(serialize, (a="Hello World",b=2))
"N0pMBwQAAAA0EAEKTmFtZWRUdXBsZR9OAQRNYWlum0QCAAAAFAJmZxBTH04BBE1haW6bRAIAAAAAIQAIIQtIZWxsbyBXb3JsZOE="
julia> deserialize(IOBuffer(base64decode(test)))
(a = "Hello World", b = 2)
The other formats mentioned were designed for long-term storage (JLD/JLD2 in particular are based on HDF5 which has file readers implemented in other languages as well) whereas serialization was designed to transfer objects/code between different Julia processes running concurrently (if I understand correctly) and so the format was never deemed stable for long-term storage since it changed every now and again. Plus, you can’t read the data in other languages as far as I’m aware.
The plus side about serialization is that since it what Julia’s processes rely on for communication, it should work with anything that you can throw at it.