serialize
serialize(stream, value)
Write an arbitrary value to a stream in an opaque format, such that it can be read back by deserialize
.
The read-back value will be as identical as possible to the original.
In general, this process will not work if the reading and writing are done by different versions of Julia,
or an instance of Julia with a different system image.
Ptr
values are serialized as all-zero bit patterns (NULL
).
Examples
julia> io = IOBuffer(); # Create an IOBuffer to write the serialized data to
julia> serialize(io, [1, 2, 3]);
julia> bytes = takebuf_array(io); # Get the serialized data as a byte array
julia> io = IOBuffer(bytes); # Create a new IOBuffer to read the serialized data from
julia> deserialized_value = deserialize(io); # Deserialize the data
julia> deserialized_value
3-element Array{Int64,1}:
1
2
3
In this example, we serialize an array [1, 2, 3]
using serialize
and write it to an IOBuffer
named io
. Then we retrieve the serialized data as a byte array using takebuf_array
.
Next, we create a new IOBuffer
named io
and initialize it with the serialized data. Finally, we use deserialize
to read and deserialize the data from io
, obtaining the original value [1, 2, 3]
in deserialized_value
.
It's important to note that the serialized data can be written to any stream (IOBuffer
, File
, etc.), and the deserialization process can read from the appropriate stream as well.
It's worth mentioning that deserialization may not work if the reading and writing are done by different versions of Julia or if there are differences in the system image. The serialize
and deserialize
functions should generally be used within the same Julia environment or compatible environments.
Additionally, Ptr
values are serialized as all-zero bit patterns (NULL
) since they are not portable across different environments due to memory addressing differences.
See Also
deserialize, eachline, eof, fd, flush, IOBuffer, ismarked, isopen, isreadonly, mark, nb_available, open, pipeline, position, read, read!, readavailable, readbytes, readbytes!, readline, redirect_stderr, redirect_stdin, reset, seek, seekend, seekstart, serialize, skip, skipchars, TextDisplay, unmark, write, writemime,User Contributed Notes
Add a Note
The format of note supported is markdown, use triple backtick to start and end a code block.