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

Custom map key sort functions #384

Open
zachelrath opened this issue Jul 9, 2019 · 0 comments
Open

Custom map key sort functions #384

zachelrath opened this issue Jul 9, 2019 · 0 comments

Comments

@zachelrath
Copy link

@zachelrath zachelrath commented Jul 9, 2019

Currently, when SortMapKeys is set to true in Config, map keys are sorted using a standard alphanumeric sort. It should be possible to specify a custom map key sorting function to use, in order to prioritize certain map keys over others.

Example: Standard SortMapKeys behavior:

cfg := jsoniter.Config{
   SortMapKeys: true,
}.Froze()

m := map[string]int{
   "beta": 2,
   "alpha": 1,
}
output, _ := cfg.MarshalToString(m)

==> {"alpha":1,"beta":2}

Example: custom SortMapKeys behavior:

cfg := jsoniter.Config{
   SortMapKeys: true,
}.Froze()
// Desired --- register an extension that defines a custom map key sorting function
cfg.RegisterExtension(...some extension...)

m := map[string]int{
   "trump": "should be first",
   "beta": 2,
   "alpha": 1,
}
output, _ := cfg.MarshalToString(m)

// DESIRED OUTPUT
==> {"trump":"should be first","alpha":1,"beta":2}
zachelrath pushed a commit to zachelrath/go that referenced this issue Jul 9, 2019
zachelrath pushed a commit to zachelrath/go that referenced this issue Jul 10, 2019
acofer added a commit to skuid/json-iterator-go that referenced this issue Aug 15, 2019
feature json-iterator#384: Specify custom map key sort function
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
1 participant
You can’t perform that action at this time.