-
Notifications
You must be signed in to change notification settings - Fork 215
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
Preserve record key order in JSON from Map
or JSON
types
#1813
Comments
Thanks for the report!
Normalization of Dhall expressions, which sorts the record fields, is one issue, but as you correctly point out, it's not the problem in this case. After normalization, we translate the normalized expressions to the JSON |
Thank you for responding quickly! It does not seem like the reordering is caused by hashing because it is always sorted in the output of |
However, if |
We use Apparently we could customize the field order in that step, but I don't see how to use that to solve this issue. |
Oh, so what you could do is encode your schema with the |
@sjakobi Do we lose value ordering after processing dhall expression? I think that we could enumerate every key from the dhall expression and use that enumeration to sort. In @bch29 example:
That goes on a |
@german1608 We translate these dhall-haskell/dhall-json/src/Dhall/JSON.hs Lines 877 to 926 in b9cbc63
The translation from dhall-haskell/dhall-json/src/Dhall/JSON.hs Lines 435 to 502 in b9cbc63
Yeah, that could work, but I think it would be tricky to make it reliable. I think we should rather use a JSON or YAML encoding that preserves key order. So encode |
Maybe we should just create our own YAML type. Better leave parsing and encoding to other libraries though. It's a huge mess. EDIT: I made a question about available libraries on r/haskell. |
@sjakobi: The |
Indeed. We could consider allowing some non- |
That is quite awkward for my use case because I have a few more layers of records around what I gave in the example, and those work just fine being left as dhall records. I would have to convert them all to the JSON type, which is a lot of boilerplate. |
I am trying to use dhall to generate JSON config for an application in which key order actually matters. It is a GUI program for visualizing tabular data, and the columns are configured like
where the order of the keys in the "schema" object determines the order of the columns in the GUI.
My dhall representation of this JSON object is
however,
dhall-to-json
reorders the fields in the map because "bar" sorts before "foo". Is it possible to havedhall-to-json
preserve key order in this case? I understand from #1187 that it would be very difficult to preserve key order from dhall records, but I am hoping that since the dhall normal form of a map is an ordered list, this case would be easier to handle.The text was updated successfully, but these errors were encountered: