Way to "encode" an object?

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?

thanks.

https://docs.julialang.org/en/v1/stdlib/Serialization/

1 Like

could u kindly show some examples? I can’t figure out the proper usage in the doc…

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)

Just be aware that the serialization format isn’t designed for long-term storage (i.e, it might change in the future, though probably not any time soon). Also, there are potential security issues, for example Document security properties of Serialization · Issue #32601 · JuliaLang/julia · GitHub (but this is also a problem for JLD2: Security issue: Type confusion, convert called during deserialization · Issue #117 · JuliaIO/JLD2.jl · GitHub).

3 Likes

The fact that this did not even merit a reply from the JLD2 maintainers is disturbing, and a strong signal that it may be abandonware.

2 Likes

actually, with Serialization being available since v1.1, JLD and JLD2 are not needed anymore I think.

I think you misunderstand:

  1. serialization has been around much longer in Julia,

  2. they provide different functionality.

my ignorance…

how? they’re the same if I’m saving the object into a file.

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.

1 Like