Skip to content

Commit

Permalink
implement #30151, accept filename in (de)serialize
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffBezanson committed Dec 4, 2018
1 parent 11c5680 commit 1db0621
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
-----------------------------
Expand Down
20 changes: 20 additions & 0 deletions stdlib/Serialization/src/Serialization.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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 ##

"""
Expand All @@ -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
Expand Down
7 changes: 7 additions & 0 deletions stdlib/Serialization/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 1db0621

Please sign in to comment.