Reputation: 683
I have Dict and all keys in that Dict have their own Types with the same name, so I need to get the Key but not as String, as Type.
toStruct function get two parameters, the first is the Type (in my case is the same as the Key) and the second is the Dict
julia> jsonDict
Dict{String, Any} with 2 entries:
"TypeExample1" => Dict{String, Any}("attr" => 5)
"TypeExample2" => Dict{String, Any}("attr" => 10)
struct TypeExample1
attr
end
struct TypeExample2
attr
end
for (key, value) in jsonDict
ToStruct.tostruct( Key, key)
end
the code above would not work obviously cause Key is a String the output should be 2 structs (TypeExample1,TypeExample2) with the assigned values
PS: it would work like this:
ToStruct.tostruct( TypeExample1, jsonDict["TypeExample1"])
but I need to automate this process
Upvotes: 0
Views: 295
Reputation: 2162
The quick and dirty way to achieve this is to use eval
.
julia> eval(Symbol("Int"))
Int64
This is of course not limited to types but works for any variable and should not be used on untrusted text.
By using the eval of a specific module (Module.eval
) you can evaluate the symbol in that module.
Upvotes: 1