# Way to "encode" an object?

**URL:** <https://discourse.julialang.org/t/way-to-encode-an-object/26465>\
**Category:** New to Julia\
**Created:** [July 17, 2019, 2:44pm UTC](https://discourse.julialang.org/t/way-to-encode-an-object/26465 "2019-07-17T14:44:05Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)\
**Post date:** [July 17, 2019, 2:44pm UTC](https://discourse.julialang.org/t/way-to-encode-an-object/26465/1 "2019-07-17T14:44:05Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [July 17, 2019, 2:47pm UTC](https://discourse.julialang.org/t/way-to-encode-an-object/26465/2 "2019-07-17T14:47:01Z")

</div>

[https://docs.julialang.org/en/v1/stdlib/Serialization/](https://docs.julialang.org/en/v1/stdlib/Serialization/)

---

<div class="post-metadata">

**Author:** ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)\
**Post date:** [July 17, 2019, 2:57pm UTC](https://discourse.julialang.org/t/way-to-encode-an-object/26465/3 "2019-07-17T14:57:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dawbarton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dawbarton/32/215461_2.png) [@dawbarton](https://discourse.julialang.org/u/dawbarton)\
**Post date:** [July 17, 2019, 2:58pm UTC](https://discourse.julialang.org/t/way-to-encode-an-object/26465/4 "2019-07-17T14:58:42Z")

</div>

If you want a string output, just combine the previously mentioned serialization with base64 encoding.

```julia
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](https://github.com/JuliaLang/julia/issues/32601) (but this is also a problem for JLD2: [Security issue: Type confusion, convert called during deserialization · Issue #117 · JuliaIO/JLD2.jl · GitHub](https://github.com/JuliaIO/JLD2.jl/issues/117)).

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [July 18, 2019, 5:29am UTC](https://discourse.julialang.org/t/way-to-encode-an-object/26465/5 "2019-07-18T05:29:58Z")

</div>

> [@dawbarton](#):
>
> but this is also a problem for JLD2

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.

---

<div class="post-metadata">

**Author:** ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)\
**Post date:** [July 18, 2019, 5:46am UTC](https://discourse.julialang.org/t/way-to-encode-an-object/26465/6 "2019-07-18T05:46:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [July 18, 2019, 5:49am UTC](https://discourse.julialang.org/t/way-to-encode-an-object/26465/7 "2019-07-18T05:49:55Z")

</div>

I think you misunderstand:

1. serialization has been around much longer in Julia,

2. they provide different functionality.

---

<div class="post-metadata">

**Author:** ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)\
**Post date:** [July 18, 2019, 8:24am UTC](https://discourse.julialang.org/t/way-to-encode-an-object/26465/8 "2019-07-18T08:24:21Z")

</div>

> [@Tamas\_Papp](#):
>
> - serialization has been around much longer in Julia,

my ignorance…

> [@Tamas\_Papp](#):
>
> - they provide different functionality.

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

---

<div class="post-metadata">

**Author:** ![dawbarton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dawbarton/32/215461_2.png) [@dawbarton](https://discourse.julialang.org/u/dawbarton)\
**Post date:** [July 18, 2019, 8:35am UTC](https://discourse.julialang.org/t/way-to-encode-an-object/26465/9 "2019-07-18T08:35:18Z")

</div>

> [@tomtom](#):
>
> 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.
