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

Output with consistent key ordering #15

Open
zostay opened this issue Apr 17, 2019 · 6 comments
Open

Output with consistent key ordering #15

zostay opened this issue Apr 17, 2019 · 6 comments

Comments

@zostay
Copy link

zostay commented Apr 17, 2019

Every time you call .to-json, this class outputs the keys in a different order. It would be very helpful if .to-json always output the keys in a fixed order, especially for keeping git patches more helpfully concise.

In an ideal world, we could all agree on what that order was and it probably wouldn't alphabetical because the most important information is problem name, version, authors, license, source, with depends, resources, and other bits toward the bottom, but even alphabetical order would be better than order by random.

It should be possible to make it alphabetical by passing the :sorted-keys option to JSON::Fast, which ultimate performs the serialization. However, that has to go through JSON::Class and JSON::Marshal somehow.

@jonathanstowe
Copy link
Owner

jonathanstowe commented Apr 18, 2019

Passing :sorted-keys from JSON::Class -> JSON::Marshal -> JSON::Fast is eminently doable, however short of making a new to-json for JSON::Marshall (which I'm not inclined to do,) having the keys in a user specified order would need to be supported in ```JSON::Fast````

jonathanstowe added a commit to jonathanstowe/JSON-Marshal that referenced this issue Apr 18, 2019
jonathanstowe added a commit to jonathanstowe/JSON-Class that referenced this issue Apr 18, 2019
@jaguart
Copy link

jaguart commented Aug 14, 2022

  • Shame we can't pass in a comparison-sub where $a and $b have been set to the keys (nested / delimited)
  • For me, I want my META6.json organised where required keys come before the optional and at the end, the rarely-used-by-me :)
  • P.S. I realise this is really a JSON::Fast request

@jonathanstowe
Copy link
Owner

Oh I totally agree, if I was editing the META6 by hand I'd put the name, description , version etc at the top and it's a pain that this gets undone.

However I really can't see a way of making this happening without the support of the thing that actually produces the JSON string.

@jaguart
Copy link

jaguart commented Sep 13, 2022

See https://gist.github.com/jaguart/bd64157bcfa8271ad5bed69a286e0613 - I'm thinking you could override the .sort method on the hash to get JSON::Fast to produce consistent ordering. Apologies for newbie code - but the concept seems to work...

@lizmat
Copy link

lizmat commented Sep 13, 2022

FYI: timo/json_fast#83

@jonathanstowe
Copy link
Owner

Obviously the majority of the changes would need to be in https://github.com/jonathanstowe/JSON-Class and https://github.com/jonathanstowe/JSON-Marshal and the basic requirement, as I see it, is that a class passed to the marshal of JSON::Marshal can define itself which order the keys are output or the order of the keys can be specified there as an argument, once all that is in place it's basically implement the default order for META6 (because the to-json is that of JSON::Class, any arbitrary ordering specified by the user comes for free.) I actually have a slightly different plan but it's waiting on a PR on another module at the moment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants