Canonical (alphabetical) key ordering in JSON output/Canonization of json #1834
-
I'm currently using Glaze for JSON serialization in C++, and I'm looking to produce canonical JSON output [RFC 8785 (JSON Canonicalization Scheme)](https://cyberphone.github.io/ietf-json-canon/). This can be usefull if we need to sign the json, and changing the order might affect the signature. What is canonised JSON form:
I’m aware that:
What I wantedA way to achieve: glz::opts{ .canonical = true } // That'd imply overriding the .prettify = false so might not be a great idea, also would require knowing RFC 8785
// or
glz::opts{ .sort_keys = true } // Cleaner meaning
glz::optis{ .normalize = true } Or maybe even providing custom key sorter, Unfortunately I am not too acquainted with …which would:
Examples from other languagesOther languages have the support for this, but they do use different containers
Questions
Also just wanted to thank you for answering questions, and just keeping up the great work, because of your library I know so much more about modern c++. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
If you use a std::map then keys will be ordered. Likewise glz::json_t orders keys and uses a std::map underneath. Using a glz::meta for your structs allows you to define a canonical order even if the fields aren't in that order. But, it requires writing out a glz::meta for all your structs. It would not be too difficult to write out ordered keys for purely reflected structs. This is not currently supported and I'll look into adding a compile time option to enable this. For now, just order the keys how you like manually in a glz::meta or use a std::map or glz::json_t |
Beta Was this translation helpful? Give feedback.
If you use a std::map then keys will be ordered. Likewise glz::json_t orders keys and uses a std::map underneath.
Using a glz::meta for your structs allows you to define a canonical order even if the fields aren't in that order. But, it requires writing out a glz::meta for all your structs.
It would not be too difficult to write out ordered keys for purely reflected structs. This is not currently supported and I'll look into adding a compile time option to enable this.
For now, just order the keys how you like manually in a glz::meta or use a std::map or glz::json_t