Cannot convert JLD.AssociativeWrapper to Dict

New to julia so might be missing some basic things.

I’m trying to make the current version of the JLD package work with some older code I have and running into a type issue. The older code works with some older version of JLD but for other reasons I need to use the latest JLD and hoping to fix the old code to work with it.

The old code that is causing the problem is below. I’m not very familiar with JLD but whatever the type of index is after JLD.save(...) makes it incompatible with the array of dictionaries indexes. When I get to the last line push!(indexes, index) I get the following error:

Cannot 'convert' an object of type getfield(JLD, Symbol("##JLD.AssociativeWrapper{Core.Any,Core.Any,Base.Dict{Core.Any,Core.Any}}#378")) to an object of type Dict

    indexes = Dict[]
    for (i, index_filepath) in enumerate(indexes_filepaths)
        # check if index already created
        # if so, load it
        # if not, create and save it
        if !isfile(index_filepath)
            index = index_ngsim_trajectory(filepaths[i], minlength=minlength)
            JLD.save(index_filepath, "index", index)
            # write this information to an hdf5 file for use in python
            # the reason we write two files is that it's convenient to load 
            # via jld a dictionary that can be used directly
            ids_filepath = replace(index_filepath, ".jld", "-ids.h5")
            ids = convert(Array{Int}, collect(keys(index)))
            ts = Int[]
            te = Int[]
            for id in ids
                push!(ts, index[id]["ts"])
                push!(te, index[id]["te"])
            end
            h5open(ids_filepath, "w") do file
                write(file, "ids", ids)  
                write(file, "ts", ts)
                write(file, "te", te)
            end
        else
            index = JLD.load(index_filepath)["index"]
        end

        # load index
        push!(indexes, index)

I’m hoping I can just change the type of indexes to something that supports this new JLD type but so that the base type of the indexes array is still Dictionary-like but not sure what that would be. I tried indexes = AbstractDict[] but got the same error.

I also tried indexes = JLD.AssociativeWrapper{Core.Any, Core.Any, Dict{Core.Any, Core.Any}}[] since JLD.AssociativeWrapper looks a lot like a dictionary but this gave rise to some other errors.

Cannot 'convert' an object of type getfield(JLD, Symbol("##JLD.AssociativeWrapper{Core.Any,Core.Any,Base.Dict{Core.Any,Core.Any}}#378")) to an object of type JLD.AssociativeWrapper{Any,Any,Dict{Any,Any}}

I tried to write my own convert to call before push!(indexes, index) below but it still says no method matching convert, maybe my convert is wrong?

function convert(::Type{Dict}, x::JLD.AssociativeWrapper{K,V,T}) where {K,V,T}
    keys = x.keys
    values = x.values
    Dict(zip(keys, values))
end

Any help is greatly appreciated!

2 Likes