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: set key converter on en/decoder #5680

Closed
eaigner opened this issue Jun 11, 2013 · 7 comments
Closed

encoding/json: set key converter on en/decoder #5680

eaigner opened this issue Jun 11, 2013 · 7 comments

Comments

@eaigner
Copy link
Contributor

eaigner commented Jun 11, 2013

Instead of having to specify `json:"lowercase_field"` or similar on every
struct field, the JSON en/decoder could have a

    SetKeyConverter(c KeyConverter)

method. The KeyConverter would be responsible for converting struct/map keys to JSON
keys and vice versa.

    type KeyConverter interface {
        EncodeKey(key string) string
        DecodeKey(key string) string
    }

This would be much less intrusive into type declarations, and generating a different
JSON output would simply be a matter of using another KeyConverter instead of a whole
new struct.
@robpike
Copy link
Contributor

robpike commented Jun 11, 2013

Comment 1:

Labels changed: added priority-later, feature, packagechange, removed priority-triage.

Status changed to Accepted.

@eaigner
Copy link
Contributor Author

eaigner commented Jun 11, 2013

Comment 2:

Even better would be if an encoding could be specified instead of just a key converter
    type Encoding interface {
        Encode(key string, value interface{}) (string, interface{})
        Decode(key string, value interface{}) (string, interface{})
    }
That would also allow for custom value encoding, e.g. if time.Time should be converted
to a timestamp, or entirely custom  types.

@rsc
Copy link
Contributor

rsc commented Nov 27, 2013

Comment 3:

Labels changed: added go1.3maybe.

@rsc
Copy link
Contributor

rsc commented Nov 27, 2013

Comment 4:

Labels changed: removed feature.

@rsc
Copy link
Contributor

rsc commented Dec 4, 2013

Comment 5:

Labels changed: added release-none, removed go1.3maybe.

@rsc
Copy link
Contributor

rsc commented Dec 4, 2013

Comment 6:

Labels changed: added repo-main.

@rsc rsc added this to the Unplanned milestone Apr 10, 2015
@bradfitz
Copy link
Contributor

bradfitz commented Feb 2, 2017

Sorry, the json package is pretty frozen at this point. We need to stop accreting features and options.

Everything will be up for reconsideration if/when we do Go 2, though.

@bradfitz bradfitz closed this as completed Feb 2, 2017
@golang golang locked and limited conversation to collaborators Feb 2, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants