User provided mapping
jdonnerstag opened this issue · comments
Reading the doc I couldn't figure out how to map json keys to object properties where they are different. Unfortunately occassionally it is required with our evolving json config files (which are neither only small nor flat)
struct Foo
attr_1
var_2
end
{ "var1": 1, "var_2": 2 }
How can I map var1 onto attr_1 ?
Maintaining 2 structs of everything and then copy/mapping them is awkward. May be if users could provide a mapper ((path, old_key) -> new_key) that gets invoked before assigning the value to an attribute?
thanks a lot for your help.
See the docs for StructTypes.names
, which JSON3 uses (and respects) internally. i.e. if you define StructTypes.names(::Type{Foo}) = ((:attr_1, :var1),)
, then it will expect var1
when parsing json, but set the attr_1
field with the value for var1
. Note that your struct has to be mutable in this case, so mutable struct Foo
.