kcl-lang / kcl

KCL Programming Language (CNCF Sandbox Project). https://kcl-lang.io
https://kcl-lang.io
Apache License 2.0
1.67k stars 118 forks source link

Option to not sort keys within a json object #1258

Closed steeling closed 6 months ago

steeling commented 6 months ago

Currently, when running kcl <file> --format json, any field within a json object is sorted by key. There's a --sort_keys flag, but I believe that only applies on top level keys. It would be nice to not sort keys within an object.

This would make things easier to diff against outputs that might do their own formatting/ordering of fields

Peefy commented 6 months ago

I've opened a PR https://github.com/kcl-lang/cli/pull/72 to fix the default order for the json format output. sort_keys means that kcl will sort all attribute recursively. If we want to only sort the top-level attributes, we may need an additional parameter such as sort_top_keys or open custom sorting capabilities. Of course, you can also use tools such as jq to format the output.

steeling commented 6 months ago

what i'm seeing is that even specifying sort_keys=false is still sorting all the nested keys

Peefy commented 6 months ago

what i'm seeing is that even specifying sort_keys=false is still sorting all the nested keys

Yes. I have fixed it in this PR https://github.com/kcl-lang/cli/pull/72