diff --git a/stdlib/Serialization/src/Serialization.jl b/stdlib/Serialization/src/Serialization.jl index ee40ebdd4abad..32279e0283ede 100644 --- a/stdlib/Serialization/src/Serialization.jl +++ b/stdlib/Serialization/src/Serialization.jl @@ -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 ## """ @@ -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 diff --git a/stdlib/Serialization/test/runtests.jl b/stdlib/Serialization/test/runtests.jl index e341c6e3eb9ec..27c78f92f5e54 100644 --- a/stdlib/Serialization/test/runtests.jl +++ b/stdlib/Serialization/test/runtests.jl @@ -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)