Open elv-gilles opened 2 months ago
The challenge is that other code might call a T.UnmarshalJSONV2
method directly without going through json.Unmarshal
. In such a situation, it would be strange for them to get a json.SkipFunc
error as they would then need to write something like:
if err := v.UnmarshalJSONV2(dec, opts); err != nil {
if err == json.SkipFunc {
err = json.UnmarshalDecode(dec, &v, opts)
}
if err != nil {
return err
}
}
Now, you could ask why someone would directly call the UnmarshalJSON
or UnmarshalJSONV2
methods (over calling the json.Unmarshal
function). I don't have a great answer for that, but empirical evidence shows that it is decently common.
Now, you could ask why
Yea, I would :-) ...
Thanks for your answer, looks reasonable.
Say a struct config is defined with a function to initialise default values. With a large hierarchy, this is useful such that only relevant or mandatory fields have to be filled by users.
This is the code used with
encoding/json
With json/v2 (
jsonexp
), the following are possible:or
but the below is not possible and returns an error
unmarshal method cannot be skipped
.I believe the error is correct for V1 un/marshalling methods or text un/marshalling, but I would think logical that V2 un/marshalling methods be functionally equivalent to UnmarshalFuncV2.
This commit 6510be0 of my
json-experiment
fork has changes showing this looks possible.What do you think ?