Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement #30151, accept filename in (de)serialize #30267

Merged
merged 1 commit into from
Dec 5, 2018
Merged
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
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"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I were about to blindly comment that this was missing, but here it is! ❤️

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