Skip to content

Commit fe30634

Browse files
committed
Disable direct serialization of GPModel instance (fix #35)
1 parent ebcbf84 commit fe30634

3 files changed

Lines changed: 12 additions & 1 deletion

File tree

Project.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Match = "7eb4fadd-790c-5f42-8a69-bfa0b872bfbf"
1616
Parameters = "d96e819e-fc66-5662-9728-84c9c7592b0a"
1717
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
1818
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
19+
Serialization = "9e88b42a-f829-5b0c-bbe9-9e923198166b"
1920
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
2021

2122
[compat]
@@ -31,8 +32,8 @@ Statistics = "1"
3132
julia = "1.8"
3233

3334
[extras]
34-
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
3535
Serialization = "9e88b42a-f829-5b0c-bbe9-9e923198166b"
36+
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
3637

3738
[targets]
3839
test = ["Test", "Serialization"]

src/api.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import DataFrames
1616
import Distributions
1717
import Random
18+
import Serialization
1819

1920
import Gen
2021

@@ -868,6 +869,12 @@ function GPModel(m::Base.Dict{String, Any})
868869
m["y_transform"])
869870
end
870871

872+
# Disable direct serialization.
873+
function Serialization.serialize(path::String, model::GPModel)
874+
throw(ArgumentError("Do not use serialize(path, model:GPModel), instead use" *
875+
"use serialize(path, Dict(model)."))
876+
end
877+
871878
# https://juliaio.github.io/JLD2.jl/stable/customserialization/
872879
# struct GPModelSerialization x::Dict end
873880
# JLD2.writeas(::Type{GPModel}) = GPModelSerialization

test/test_serialize.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,7 @@ end
8484

8585
end
8686

87+
# Ensure direct serialization fails.
88+
@test_throws ArgumentError serialize("model.autogp", model1)
89+
8790
end

0 commit comments

Comments
 (0)