# Snippet for serialization of any primitive type

**URL:** <https://discourse.julialang.org/t/snippet-for-serialization-of-any-primitive-type/66304>\
**Category:** General Usage\
**Tags:** serialization\
**Created:** [August 12, 2021, 10:25pm UTC](https://discourse.julialang.org/t/snippet-for-serialization-of-any-primitive-type/66304 "2021-08-12T22:25:09Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![iamed2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iamed2/32/215082_2.png) [@iamed2](https://discourse.julialang.org/u/iamed2)\
**Post date:** [August 12, 2021, 10:25pm UTC](https://discourse.julialang.org/t/snippet-for-serialization-of-any-primitive-type/66304/1 "2021-08-12T22:25:09Z")

</div>

[https://gist.github.com/iamed2/7800a9b391d9e11f0f2b7fbbfbb84c32](https://gist.github.com/iamed2/7800a9b391d9e11f0f2b7fbbfbb84c32)

I recently came up with this for [InlineString](https://github.com/JuliaData/WeakRefStrings.jl/pull/78) and realized it is nearly universally applicable for primitive types.

[Standard library Julia serialization](https://github.com/JuliaLang/julia/blob/master/stdlib/Serialization/src/Serialization.jl) works well by default for primitive types, and will just call [read](https://github.com/JuliaLang/julia/blob/f19b9a20dde2688c642b7dd709b5d9446e4df2f6/stdlib/Serialization/src/Serialization.jl#L1428) and [write](https://github.com/JuliaLang/julia/blob/f19b9a20dde2688c642b7dd709b5d9446e4df2f6/stdlib/Serialization/src/Serialization.jl#L664). If you have simple symmetric read and write functions for your primitive types, you should be good to go without these additional methods.

If for some reason you:

- don’t have or want both read and write functions
- have read and write functions that aren’t round-trippable
- have read and write functions that transform the data before writing in a way that is either expensive or produces an inefficient data representation

then this serialization method may be right for you!
