From 1db06216be7d4ce0ba14ca81b22ad486b6c02fbe Mon Sep 17 00:00:00 2001 From: Jeff Bezanson Date: Tue, 4 Dec 2018 14:56:50 -0500 Subject: [PATCH] implement #30151, accept filename in (de)serialize --- NEWS.md | 1 + stdlib/Serialization/src/Serialization.jl | 20 ++++++++++++++++++++ stdlib/Serialization/test/runtests.jl | 7 +++++++ 3 files changed, 28 insertions(+) diff --git a/NEWS.md b/NEWS.md index 6ef728afdf65e..cecead8a800eb 100644 --- a/NEWS.md +++ b/NEWS.md @@ -63,6 +63,7 @@ Standard library changes * `randperm` and `randcycle` now use the type of their argument to determine the element type of the returned array ([#29670]). * A new method `rand(::Tuple)` implements sampling from the values of a tuple ([#25278]). + * `serialize` and `deserialize` now accept a filename argument, like `write` and `read` ([#30151]). Compiler/Runtime improvements ----------------------------- diff --git a/stdlib/Serialization/src/Serialization.jl b/stdlib/Serialization/src/Serialization.jl index 41fd2ccf34d56..6dbe517a7ad6a 100644 --- a/stdlib/Serialization/src/Serialization.jl +++ b/stdlib/Serialization/src/Serialization.jl @@ -694,6 +694,16 @@ function serialize(s::IO, x) serialize(ss, x) end +""" + serialize(filename::AbstractString, value) + +Open a file and serialize the given value to it. + +!!! compat "Julia 1.1" + This method is available as of Julia 1.1. +""" +serialize(filename::AbstractString, x) = open(io->serialize(io, x), filename, "w") + ## deserializing values ## """ @@ -707,6 +717,16 @@ the integrity and correctness of data read from `stream`. """ deserialize(s::IO) = deserialize(Serializer(s)) +""" + deserialize(filename::AbstractString) + +Open a file and deserialize its contents. + +!!! compat "Julia 1.1" + This method is available as of Julia 1.1. +""" +deserialize(filename::AbstractString) = open(deserialize, filename) + 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 514e5388741e3..dd275d227dd7a 100644 --- a/stdlib/Serialization/test/runtests.jl +++ b/stdlib/Serialization/test/runtests.jl @@ -532,3 +532,10 @@ let io = IOBuffer() f2 = deserialize(io) @test f2(1) === 1f0 end + +# using a filename; #30151 +let f = tempname(), x = [rand(2,2), :x, "hello"] + serialize(f, x) + @test deserialize(f) == x + rm(f) +end