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

Open
superloach opened this issue May 7, 2020 · 4 comments
Open

proposal: encoding/json: debug printing #38935

superloach opened this issue May 7, 2020 · 4 comments
Labels
Milestone

Comments

@superloach
Copy link

@superloach superloach commented May 7, 2020

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
@gopherbot gopherbot added the Proposal label May 7, 2020
@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented May 7, 2020

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

@superloach
Copy link
Author

@superloach 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

@ianlancetaylor ianlancetaylor commented May 8, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants
You can’t perform that action at this time.