Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions stdlib/Serialization/src/Serialization.jl
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,22 @@ Open a file and serialize the given value to it.
"""
serialize(filename::AbstractString, x) = open(io->serialize(io, x), filename, "w")

"""
serialize(value) -> Vector{UInt8}

Serialize the given value to a byte vector and return it.

See [`serialize(::IO, value)`](@ref serialize(::IO, ::Any)) for more details on serialization format and caveats.

!!! compat "Julia 1.13"
This method is available as of Julia 1.13.
"""
function serialize(x)::Vector{UInt8}
buf = IOBuffer()
serialize(buf, x)
return take!(buf)
end

## deserializing values ##

"""
Expand All @@ -847,6 +863,18 @@ Open a file and deserialize its contents.
"""
deserialize(filename::AbstractString) = open(deserialize, filename)

"""
deserialize(data::Vector{UInt8})

Deserialize the given byte vector and return the resulting value.

See [`deserialize(::IO)`](@ref deserialize(::IO)) for more details and caveats.

!!! compat "Julia 1.13"
This method is available as of Julia 1.13.
"""
deserialize(data::Vector{UInt8}) = deserialize(IOBuffer(data))

function deserialize(s::AbstractSerializer)
handle_deserialize(s, Int32(read(s.io, UInt8)::UInt8))
end
Expand Down
6 changes: 6 additions & 0 deletions stdlib/Serialization/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,12 @@ end
@test l2.parts === ()
end

# serialize to and deserialize from Vector{UInt8}
@testset "Vector{UInt8}" begin
@test serialize([1, 2, 3]) isa Vector{UInt8}
@test deserialize(serialize([1, 2, 3])) == [1, 2, 3]
end

@testset "Docstrings" begin
undoc = Docs.undocumented_names(Serialization)
@test_broken isempty(undoc)
Expand Down