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

Comments

Projects
None yet
5 participants
@eaigner
Copy link
Contributor

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

This comment has been minimized.

Copy link
Contributor

commented Jun 11, 2013

Comment 1:

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

Status changed to Accepted.

@eaigner

This comment has been minimized.

Copy link
Contributor Author

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

This comment has been minimized.

Copy link
Contributor

commented Nov 27, 2013

Comment 3:

Labels changed: added go1.3maybe.

@rsc

This comment has been minimized.

Copy link
Contributor

commented Nov 27, 2013

Comment 4:

Labels changed: removed feature.

@rsc

This comment has been minimized.

Copy link
Contributor

commented Dec 4, 2013

Comment 5:

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

@rsc

This comment has been minimized.

Copy link
Contributor

commented Dec 4, 2013

Comment 6:

Labels changed: added repo-main.

@rsc rsc added this to the Unplanned milestone Apr 10, 2015

@bradfitz

This comment has been minimized.

Copy link
Member

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 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.
You can’t perform that action at this time.