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

Have one mechanism to produce json with sorted keys instead of N different ways #1650

Open
mkustermann opened this issue Oct 11, 2024 · 0 comments

Comments

@mkustermann
Copy link
Member

Currently there's many different places in the code that look like this:

  Map<String, Object?> toJson() => {
        for (final key in encoding.keys) key: encoding[key],
        _typeKey: type,
      }..sortOnKey();

Often this property isn't even used. But when it's used it's problematic because:

  • it's error prone: As every single toJson() implementation would need to remember this and it's easy to forget to add this
  • it's redundant code: We have this in many different places.

I suggest we centralize this into one mechanism that can be generally used. A user that requires keys to be sorted can then call it and is guaranteed to get recursively sorted maps in the entire json, e.g.

Map<String, Object?> json;

final jsonSorted = ensureSortedJsonMaps(json); // <-- recursively walks the tree, sorting any maps when needed
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

1 participant