JSON3: ERROR: MethodError: no method matching Array{Int64,2}(::Array{Int64,1})

I asked that some time ago, and I don’t think so.

In your specific case, since your structure is mutable, you can directly reshape that field with obj.par3 = reshape(obj.par3,2,3). My issue was worse, because my structure was immutable, and I had to define another structure just to read the json file with the same fields, but with a linear array, and then initialize the actual data structure from this temporary structure with linearized arrays.

By the way, my solution ended up not being very elegant, and someone can point to a better solution which might be useful to you as well. What I did is to define four macros, one with the fields before the matrix, two with either the matrix or the linearized form (a vector) and a third with the fields after that matrix. Something like:

macro Results_Start()
  ex = quote
    data1
    data2 
  end; esc(ex)
end
macro Results_Matrix()
  ex = quote
    atoms :: Array{Float64,2}
  end; esc(ex)
end
macro Results_Vector()
  ex = quote
    atoms :: Vector{Float64}
  end; esc(ex)
end
macro Results_End()
  ex = quote
    data4
    data5
  end; esc(ex)
end

Then I define two structs, with these macros, which share all the fields except the matrix or vector one:

struct Result
  @Result_Start()
  @Result_Matrix()
  @Result_End()
end

mutable struct MutableResult
  @Result_Start()
  @Result_ector()
  @Result_End()
end

The MutableResult struct is only used for reading the JSON file, reshape that matrix and initialize the immutable struct with

  return Result([getfield(R,field) for field in fieldnames(Result)]...)

All this seems pretty ugly, so I guess there are better solutions.

1 Like