Julia Serialization Tutorial
last modified November 18, 2026
Serialization is the process of turning a live value into a sequence of
bytes that can be stored or transmitted. Deserialization reads those
bytes back and reconstructs the value. Julia's Serialization
standard library implements this for Julia data, preserving types rather than
just text.
This tutorial shows how to serialize values to an IOBuffer or a
file, how to round-trip composite values, and what limitations apply. The
examples were verified with Julia 1.13.4. Input and output streams are
introduced in Julia IO.
Serializing a Value
The serialize function writes a value to a stream and
deserialize reads it back. An IOBuffer is an ideal
target because it keeps everything in memory. The seekstart
function moves the stream position back to the beginning before reading.
using Serialization io = IOBuffer() serialize(io, 42) seekstart(io) x = deserialize(io) println(x) println(typeof(x))
Writing the integer 42 and reading it back returns the same value with the same
type, Int64. A round trip of this kind preserves the exact type of
the object, unlike writing its textual representation, which would lose that
information.
$ julia main.jl 42 Int64
Round-Tripping a Custom Type
Composite values can be serialized as well. As long as the type is defined in the current session, Julia can reconstruct an instance with all of its fields intact.
using Serialization
struct Point
x::Int
y::Int
end
Base.show(io::IO, p::Point) = print(io, "Point(", p.x, ", ", p.y, ")")
io = IOBuffer()
serialize(io, Point(3, 4))
seekstart(io)
p = deserialize(io)
println(p)
println(p == Point(3, 4))
A Point is written to the buffer and read back as a new object of
the same type. The equality check is true, confirming that both fields were
restored. Defining a show method, as described in
Julia IO, simply makes the printed result readable;
it is not required for serialization.
$ julia main.jl Point(3, 4) true
Storing Multiple Values
A single stream can hold any number of values. Each call to
serialize appends one value, and each call to
deserialize consumes the next one in the same order.
using Serialization io = IOBuffer() serialize(io, [1, 2, 3]) serialize(io, "hello") seekstart(io) println(deserialize(io)) println(deserialize(io))
The array is written first and the string second. Reading them back in sequence
reproduces both values. The stream must be rewound with seekstart
before reading, otherwise it would still be positioned at the end and no data
would be available.
$ julia main.jl [1, 2, 3] hello
Round-Tripping a Tuple
Because types are preserved, heterogeneous containers such as tuples come back with exactly the same element types they had before serialization.
using Serialization io = IOBuffer() serialize(io, (1, "two", 3.0, :four)) seekstart(io) t = deserialize(io) println(t) println(typeof(t))
The tuple contains an integer, a string, a floating-point number and a symbol.
After the round trip its type is still
Tuple{Int64, String, Float64, Symbol}, showing that each element
kept its original type.
$ julia main.jl
(1, "two", 3.0, :four)
Tuple{Int64, String, Float64, Symbol}
Writing to a File
The same functions work with an IOStream opened for a file. The
example below uses a path under /tmp/opencode/ so that nothing is
written into the repository, and removes the file when it is done.
using Serialization
path = "/tmp/opencode/julia_ser_demo.bin"
open(path, "w") do io
serialize(io, (answer = 42, name = "Julia"))
end
open(path, "r") do io
println(deserialize(io))
end
rm(path)
The do form of open closes the file automatically at
the end of the block. A named tuple is written in one pass and read back in
another, and rm deletes the temporary file. File access itself is
covered in Julia files.
$ julia main.jl (answer = 42, name = "Julia")
Closures and Opaque Types
Julia can serialize a closure, but the result is only meaningful in a process that has the same code available. Within one session the restored function behaves like the original.
using Serialization
f = let a = 10
x -> x + a
end
io = IOBuffer()
serialize(io, f)
seekstart(io)
g = deserialize(io)
println(g(5))
The closure captures the value a = 10, and the restored function
still adds ten to its argument, so calling it with five prints fifteen. In a
different program, functions and closures are serialized by reference to their
code; if that code is not loaded, deserialization will fail. Types defined
inside a local scope, anonymous types and other opaque objects have the same
restriction.
$ julia main.jl 15
Versioning Caveats
Serialized data is a snapshot of Julia's internal object representation, not a portable interchange format. The byte layout can change between Julia releases, so you should not rely on data serialized with one version being readable by another. Treat serialized files as temporary caches that are regenerated by the program that needs them.
If data must survive upgrades or be read by other languages, use a stable, documented format such as JSON or CSV instead. Serializing custom types also requires the same type definition to exist when the data is read; renaming or moving a type can make old data unusable.
Best Practices for Serialization
A few guidelines keep serialized data reliable.
- Use an
IOBufferfor in-memory round trips: no filesystem side effects are needed. - Keep reader and writer in sync: deserialize values in the same order they were serialized.
- Avoid long-term storage: the format is tied to a Julia version and to your type definitions.
- Prefer portable formats for exchange: use a documented format when other programs or future versions must read the data.
Source
Julia Serialization documentation
Julia I/O and Network documentation
In this article we have used Julia's Serialization library: writing values with
serialize, reading them with deserialize, round-tripping
composite values and tuples, working with files, and understanding the
limitations around closures and versions. Streams are explained in
Julia IO and custom types in
Julia structs.
Author
List all Julia tutorials.