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

encoding/json: expose more internals #22034

Closed
MOZGIII opened this issue Sep 26, 2017 · 5 comments

Comments

Projects
None yet
4 participants
@MOZGIII
Copy link

commented Sep 26, 2017

I checked out git history and found ot that https://github.com/golang/go/blob/master/src/encoding/json/tags.go has never changed significantly since in was introduced. Could we expose this logic as an API so that applications could parse JSON tags more reliably?

I want this because I'm currently implementing a kind of mapper that translates struct from one form to another, and it needs to know how particular fields will be encoded as JSON. I can't seem to filnd a way to programmatically read JSON marshaller configuration from struct field tags without relying on implementation internals.

Problem is, even though JSON tags format is documented, there's no API provided to ease interoperation with it from the other code. Would be great to have it.

@mvdan

This comment has been minimized.

Copy link
Member

commented Sep 26, 2017

I don't have an answer for you, but I can imagine that most people will oppose this. Adding more complexity and long-term burden to the exposed API is a factor against exporting any name in std, and this particular piece of code is useless to almost every encoding/json user.

One exception would be someone who wants to write an encoding/json alternative, or someone who will fiddle with the internals such as yourself. In those cases, copying the code (while keeping the license) is usually the best overall option. It's not much code, either.

@MOZGIII

This comment has been minimized.

Copy link
Author

commented Sep 26, 2017

Since I didn't expect this to be quick, I made an quick implementation here: https://github.com/MOZGIII/go-jsontag
The thing is if golang's implementation will change, things will break on my side. But for near future it's an acceptable tradeoff. Also it works as sort of concept example.
Looking forward to more opinions on this topic.

@MOZGIII

This comment has been minimized.

Copy link
Author

commented Sep 26, 2017

By the way: my usecase is to build a description of a struct (that can also include other structs), with the information for each field about it's JSON name (dot-separated for nesting) and type (string/bool/etc).

@MOZGIII

This comment has been minimized.

Copy link
Author

commented Sep 26, 2017

@mvdan Just noticed you mention keeping the license. How do I do it right? It what's in my repo ok?

@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

commented Sep 26, 2017

I agree with @mvdan. There is insufficient reason to expose this API, and it's easy to implement yourself. Implementing it yourself is not relying on encoding/json internals; it's simply handling the documented tag syntax in a way that is useful for you.

Closing the issue.

@golang golang locked and limited conversation to collaborators Sep 26, 2018

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
You can’t perform that action at this time.