native
native copied to clipboard
Have one mechanism to produce json with sorted keys instead of N different ways
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