From 55dc51f12e89ed672809c1439ce81af19045232d Mon Sep 17 00:00:00 2001
From: David Widmann <devmotion@users.noreply.github.com>
Date: Thu, 17 Sep 2020 20:04:53 +0200
Subject: [PATCH] Remove `read` and `write` (#227)

---
 Project.toml                |  2 +-
 README.md                   | 12 +++++++++---
 src/MCMCChains.jl           | 26 +++++++++++++++++++++++++-
 src/fileio.jl               | 11 -----------
 test/serialization_tests.jl |  9 +++++++--
 5 files changed, 42 insertions(+), 18 deletions(-)

diff --git a/Project.toml b/Project.toml
index 59a8707f..938601ca 100644
--- a/Project.toml
+++ b/Project.toml
@@ -3,7 +3,7 @@ uuid = "c7f686f2-ff18-58e9-bc7b-31028e88f75d"
 keywords = ["markov chain monte carlo", "probablistic programming"]
 license = "MIT"
 desc = "Chain types and utility functions for MCMC simulations."
-version = "4.2.0"
+version = "4.2.1"
 
 [deps]
 AbstractFFTs = "621f4979-c628-5d54-868e-fcf4e3e8185c"
diff --git a/README.md b/README.md
index 44a9d82f..dbf3229c 100644
--- a/README.md
+++ b/README.md
@@ -298,14 +298,20 @@ corner(c::Chains, [:A, :B])
 
 ### Saving and Loading Chains
 
-Chains objects can be serialized and deserialized using `read` and `write`.
+Like any Julia object, a `Chains` object can be saved using `Serialization.serialize`
+and loaded back by `Serialization.deserialize` as identical as possible.
+Note, however, that in general
+[this process will not work if the reading and writing are done by different versions of Julia, or an instance of Julia with a different system image](https://docs.julialang.org/en/v1/stdlib/Serialization/#Serialization-1).
+You might want to consider [JLSO](https://github.com/invenia/JLSO.jl) for saving metadata
+such as the Julia version and the versions of all packages installed as well.
 
 ```julia
 # Save a chain.
-write("chain-file.jls", chn)
+using Serialization
+serialize("chain-file.jls", chn)
 
 # Read a chain.
-chn2 = read("chain-file.jls", Chains)
+chn2 = deserialize("chain-file.jls")
 ```
 
 ### Exporting Chains
diff --git a/src/MCMCChains.jl b/src/MCMCChains.jl
index 27a24a49..d7cd40f9 100644
--- a/src/MCMCChains.jl
+++ b/src/MCMCChains.jl
@@ -20,8 +20,8 @@ import TableTraits
 import IteratorInterfaceExtensions
 
 using LinearAlgebra: diag, dot, BlasReal
-import Serialization: serialize, deserialize
 import Random
+import Serialization
 import Statistics: std, cor, mean, var, mean!
 
 export Chains, chains, chainscat
@@ -77,4 +77,28 @@ include("plot.jl")
 include("tables.jl")
 include("rstar.jl")
 
+# deprecations
+# TODO: Remove dependency on Serialization if this deprecation is removed
+@static if VERSION < v"1.1"
+    Base.@deprecate read(
+        f::AbstractString,
+        ::Type{T}
+    ) where {T<:Chains} open(Serialization.deserialize, f, "r") false
+    Base.@deprecate write(
+        f::AbstractString,
+        c::Chains
+    ) open(f, "w") do io
+        Serialization.serialize(io, c)
+    end false
+else
+    Base.@deprecate read(
+        f::AbstractString,
+        ::Type{T}
+    ) where {T<:Chains} Serialization.deserialize(f) false
+    Base.@deprecate write(
+        f::AbstractString,
+        c::Chains
+    ) Serialization.serialize(f, c) false
+end
+
 end # module
diff --git a/src/fileio.jl b/src/fileio.jl
index 04f12d6d..e294df21 100644
--- a/src/fileio.jl
+++ b/src/fileio.jl
@@ -1,16 +1,5 @@
 #################### File I/O ####################
 
-function Base.read(name::AbstractString, ::Type{T}) where {T<:Chains}
-  c = open(deserialize, name, "r")
-  isa(c, T) || throw(TypeError(:open, "read(\"$name\", $T)", T, c))
-  return c
-end
-
-function Base.write(name::AbstractString, c::Chains)
-  open(file -> serialize(file, c), name, "w")
-end
-
-
 function readcoda(output::AbstractString, index::AbstractString)
   out = readdlm(output, Any)
   ind = readdlm(index, Any)
diff --git a/test/serialization_tests.jl b/test/serialization_tests.jl
index 401634e9..40ccc193 100644
--- a/test/serialization_tests.jl
+++ b/test/serialization_tests.jl
@@ -2,6 +2,7 @@ using MCMCChains
 using Distributions
 
 using Random
+using Serialization
 using Test
 
 Random.seed!(20)
@@ -27,8 +28,12 @@ ProjDir = mktempdir()
     end
     chn1 = Chains(vals, ["m", "s"])
 
-    write(joinpath(ProjDir, "chn1.jls"), chn1)
-    chn2 = read(joinpath(ProjDir, "chn1.jls"), Chains)
+    # Julia 1.0 doesn't support `serialize(::AbstractString, value)`
+    # and `deserialize(::AbstractString)`
+    open(joinpath(ProjDir, "chn1.jls"), "w") do io
+        serialize(io, chn1)
+    end
+    chn2 = open(deserialize, joinpath(ProjDir, "chn1.jls"), "r")
 
     open(joinpath(ProjDir, "chn1.txt"), "w") do io
         describe(io, chn1);