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: allow using different tag #64194

Open
dancojocaru2000 opened this issue Nov 15, 2023 · 3 comments
Open

proposal: encoding/json: allow using different tag #64194

dancojocaru2000 opened this issue Nov 15, 2023 · 3 comments
Labels
Milestone

Comments

@dancojocaru2000
Copy link

dancojocaru2000 commented Nov 15, 2023

I think it should be possible to specify a different tag name that should be used when marshallng JSON.

An example use case would be a proxy renaming fields between an internal and external API:

type Example struct {
    CustomerAccountId string `jsonInternal:"kundenKontoId" json:"customerAccountId"`
}

When unmarshaling, the tag jsonInternal could be specified to unmarshal using the internal names, while when marshaling the normal json tag would be used (by default).

The current workaround is having two structs with identical fields but different tags, which feels messy.

@gopherbot gopherbot added this to the Proposal milestone Nov 15, 2023
@seankhliao
Copy link
Member

I think new feature requests for json should be directed to #63397 right now

@dancojocaru2000
Copy link
Author

Oh, I checked the issues but didn't think to check the discussions as well. Will post a comment there, thanks!

@AlexanderYastrebov
Copy link
Contributor

The current workaround is having two structs with identical fields but different tags, which feels messy.

An example for readers https://go.dev/play/p/nQAavLnLVn5

@seankhliao seankhliao changed the title proposal: encoding/json: Allow using different tag proposal: encoding/json: allow using different tag Dec 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Incoming
Development

No branches or pull requests

4 participants