-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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
proposal: encoding/json: debug printing #38935
Comments
Can you give a short detailed example showing how this would be useful? Thanks. |
If you're given data like... {
"foo": 123,
"bar": null,
"baz": null
} ...then there's a pretty good chance that For example, you could write something like this... type Test struct {
KnownField int `json:"foo"`
UnknownFieldA interface{} `json:"bar,debug"`
UnknownFieldB json.RawMessage `json:"baz,debug"`
} ... (showing use of either
... or, if new data was provided...
|
Why can't you just, you know, unmarshal into a variable of type |
I agree with Ian. If what you want to do is debug json's internals, then editing the json package itself to add debug prints is always possible. We could possibly make that easier with a global |
In general we don't add these kinds of debug/trace hooks to our packages. What if you wanted to debug the execution of math.Sin on a particular input? And so on. The answer is to copy and modify the code to check what you want to check. |
Ah yeah, sorry I forgot to close this. Trivial idea, looking back it's not really useful 😁 |
I apologise if any of this is incorrect.
I would like to be able to provide a
,debug
flag in a struct tag, which would instruct theencoding/json
library to print out any data marshaled/unmarshaled to/from that field, like so:This would be especially useful for writing client libraries, where some fields may be undocumented, and having logs of the values you get can help reverse-engineer things.
Using the builtin
println
function makes sense for this, in my opinion.I started making a basic attempt at implementing this with custom MarshalJSON/UnmarshalJSON methods, but quickly realised that I would basically have to implement cases for every type that
encoding/json
already supports.I then attempted to modify
encoding/json
to support this, and found myself unable to locate the code relevant to checking flags, particularly in unmarshaling.Any thoughts or tips on implementing this would be appreciated, as I intend to create a PR for this once I've figured it out. :)
The text was updated successfully, but these errors were encountered: