-
Notifications
You must be signed in to change notification settings - Fork 17.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
encoding/json: Unmarshaler breaks DisallowUnknownFields #41144
Comments
When you call |
I know that. But I do not want to generally enforce that. I want to use whatever setting is in the top level |
The problem here is that I don't think there's a way to fix this in the current API without duplicating multiple chunks of it, or breaking it entirely, which is not an option in Go 1.x. I recently collected my thoughts about an encoding/json redesign, and the issues with the options are near the top of that list, if you're interested. |
I don't control all code that calls This issue is basically about changing the api in a way that does allow passing these options down, for example via an additional, optional interface ( |
I imagine there's an issue for this already, but I honestly can't find it right now. Optionally expanding the interface is an option, but honestly not a very good one - you then move the problem to fixing all the implementations to do the right thing, which also adds quite a lot of code churn, almost like a switch to a v2 API. |
Well as mentioned, I don't have a lot of opinion on how exactly this should look like, but I imagine that a v2 api design will take very long and I'd prefer a not-so-nice solution for this in the foreseeable future over a nice solution in the very far future (also, adding something to the current api won't keep us from implementing something proper in a v2 api) |
Indeed, a redesign could take a while. The problem with not-so-nice solutions is that sometimes they're worse than not doing anything at all, and I think the consensus for now is to mostly freeze the encoding/json API as is. You can look at all the frozen json proposals for examples. @rsc @dsnet might be able to confirm with more authority than me :) If you want to propose adding to the package API, then you should turn this into a proposal and get it reviewed like any other proposed API changes. You could reuse this issue, or open a new one instead. See https://github.com/golang/proposal |
As @mvdan mentioned, I don't know if there is a specific issue about the top-level option passing problem, but it's one that I've commented about in a number of different |
Hi all, we kicked off a discussion for a possible "encoding/json/v2" package that addresses the spirit of this proposal. In v2 we propose supporting the following interface: type UnmarshalerV2 interface {
UnmarshalJSONV2(*jsontext.Decoder, Options) error
} Of particular note, this passes down an |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
Built a custom
json.Unmarshaler
to default a config file, then used ajson.Decoder
withDisallowUnknownFields
to unmarshal a json representation that had an additional, unknown field. I expected that to fail but it didn't.https://play.golang.org/p/8cEJU-Y0-9L
What did you expect to see?
An error, pointing out the unknown, additional field in the json document
What did you see instead?
No error
The text was updated successfully, but these errors were encountered: