Transferring data structures between Julia files?

I need to transfer information between two Julia files (or: between two Jupyter notebooks). Examples of data structures:

  • A dictionary with keys “a”, “b”, “c”, etc., where the value for each key is a tuple holding Floats, dates, interpolation functions, etc.
  • It is kind of ugly to write this into a *.jl file to include the file in the other document.

Question: is there a simpler way? Can I store the data in a Json file and read the JSON file in the other document? If so:

  • Which package?

Example: suppose I have the following data structure:

using Dates, DataInterpolations
#
x = 3
t = Date(2020,11,5)
X = 0:20
Y = sin.(X*2pi/10)
y = ConstantInterpolation(Y,X)
#
d = Dict("a" => (x,t,y))
# ...
merge(d,Dict("b") => ...)

If I want to store this as a *.jl file, I cannot save/write the dictionary d, but instead need to build it up from scratch in write statements… something like:

open("my_dictionary.jl", "w") do f
    write(f,"t_y = $(Year(t).value) \n")
    write(f,"t_m = $(Month(t).value) \n")
    write(f,"t_d = $(Day(t).value) \n")
    write(f,"X = $(X) \n")
    write(f,"Y = $(Y) \n")
    #
    write(f,"y = ConstantInterpolation(Y,X) \n")
    write(f,"d = Dict("a",($(x),Date(t_y,t_m,t_d),y)) \n")
end

When I run this code, I can include the file in another Jupyter notebook, and get back the dictionary:

using Dates, DataInterpolations, Plots
include("my_dictionary.jl")
plot(d["a"][3])

I assume there is a simpler way, so that I only need to write something like

save("my_dictionary",$d)

or something like this…

But what is the best package to do this?

Maybe try the JLD2.jl or BSON.jl packages?

1 Like

OK – I’ve seen a JSON package, and a JSON2 package. And then there is the JLD and the JLD2 package. And a BSON package.

But which do you recommend? Are all maintained? Simple to use vs. comprehensive?

I have used and like all of the following packages:

  1. JLD2
  2. JSON3
  3. BSON
1 Like

OK – in that order? Or all are good?

All three of the packages that I listed are good. I would recommend starting with JLD2.

2 Likes

Thanks! You have limited my need to read lots of documentation :-).

You could also look at the standard library Serialization.
https://docs.julialang.org/en/v1/stdlib/Serialization/

Might be suitable if you are always transferring across the same Julia versions.

3 Likes

I use different versions of Julia on various computers. So: v. 1.4.2 on my home computer, 1.5.1 on my other computers.

From your comment, it sounds like this might be a problem with Serialization? Or do you mean the problem is in Windows version vs. Mac version, etc.?