You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Often, we are interested in only a part of the JSON object. We want to write the types only for the relevant sections. Let there be an object with a relevant element a and a complex element b that we do not want to write out the complex type. We won't use b downstream much, but we want to still have consistent access. Automatically generated types might be overwhelming.
We can just use the Type Any for that:
using JSON3
json_string ="""{ "a" : 1, "b" : { "x" : 2, "y": "foo" }}"""struct TypeA
a::Number
b::Anyend
obj1 = JSON3.read(json_string, TypeA)
obj1.b.y # ERROR: type Dict has no field y, but consistent syntax
obj1.b["y"] # "foo", works but inconsistent syntax
This will give us element b of type Dict{String, Any}. However, we can not use the . operator to get the field here like we can do it on the parsed sections. This is inconsistent. Therefore we might want to use JSON3.Object instead of Any. A quick fix would be:
Often, we are interested in only a part of the JSON object. We want to write the types only for the relevant sections. Let there be an object with a relevant element
a
and a complex elementb
that we do not want to write out the complex type. We won't useb
downstream much, but we want to still have consistent access. Automatically generated types might be overwhelming.We can just use the Type
Any
for that:This will give us element
b
of typeDict{String, Any}
. However, we can not use the.
operator to get the field here like we can do it on the parsed sections. This is inconsistent. Therefore we might want to useJSON3.Object
instead ofAny
. A quick fix would be:Shall this feature be part of JSON3.jl? Is there a better way to solve the partial explicit typing?
The text was updated successfully, but these errors were encountered: