Skip to content
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

Closed
superloach opened this issue May 7, 2020 · 6 comments
Closed

proposal: encoding/json: debug printing #38935

superloach opened this issue May 7, 2020 · 6 comments

Comments

@superloach
Copy link

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 the encoding/json library to print out any data marshaled/unmarshaled to/from that field, like so:

type Test struct {
    SomeField interface{} `json:"some_field,debug"`
}

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. :)

@ianlancetaylor ianlancetaylor changed the title Debug printing in encoding/json proposal: encoding/json: debug printing May 7, 2020
@gopherbot gopherbot added this to the Proposal milestone May 7, 2020
@ianlancetaylor
Copy link
Contributor

Can you give a short detailed example showing how this would be useful? Thanks.

@superloach
Copy link
Author

superloach commented May 7, 2020

If you're given data like...

{
    "foo": 123,
    "bar": null,
    "baz": null
}

...then there's a pretty good chance that "bar" and "baz" can have values. Being able to log their values without having to write custom marshal/unmarshal methods would be convenient.

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 interface{} or json.RawMessage) and you would get the output...

json: wrote to field `UnknownFieldA`: `nil`
json: wrote to field `UnknownFieldB`: `null`

... or, if new data was provided...

json: wrote to field `UnknownFieldA`: `map[a:3.14]`
json: wrote to field `UnknownFieldB`: `{"a": 3.14}`

@ianlancetaylor
Copy link
Contributor

Why can't you just, you know, unmarshal into a variable of type Test and print the value? You already have to edit the code to add the ,debug tag anyhow.

@mvdan
Copy link
Member

mvdan commented May 8, 2020

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 const debug = false, but flipping that would require editing the json package anyway.

@rsc
Copy link
Contributor

rsc commented Jun 10, 2020

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.

@rsc rsc added this to Incoming in Proposals (old) Jun 10, 2020
@superloach
Copy link
Author

Ah yeah, sorry I forgot to close this. Trivial idea, looking back it's not really useful 😁

@rsc rsc moved this from Incoming to Declined in Proposals (old) Jun 24, 2020
@golang golang locked and limited conversation to collaborators Jun 10, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
No open projects
Development

No branches or pull requests

5 participants