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

Re-export serde_json's preserve_order feature #2896

Open
2 tasks done
odevices opened this issue Dec 28, 2024 · 1 comment
Open
2 tasks done

Re-export serde_json's preserve_order feature #2896

odevices opened this issue Dec 28, 2024 · 1 comment
Labels
request Request for new functionality

Comments

@odevices
Copy link

What's missing?

I am unable to preserve the order of my fields in json-serialized responses. The serde_json crate has a preserve_order feature but it is not exposed by the rocket crate.

Ideal Solution

No response

Why can't this be implemented outside of Rocket?

I believe if we want to use rocket's Json guard and have preserve_order then we are forced to re-export the feature on rocket's serde_json.

Are there workarounds usable today?

No response

Alternative Solutions

No response

Additional Context

No response

System Checks

  • I do not believe that this feature can or should be implemented outside of Rocket.
  • I was unable to find a previous request for this feature.
@odevices odevices added the request Request for new functionality label Dec 28, 2024
@the10thWiz
Copy link
Collaborator

We are not technically required to re-export the feature. If you also add an explicit dependency on serde_json, and enable the preserve_order feature, it will also enable it for Rocket.

serde_json = { version = "^10.0.26", features = ["preserve_order"] }

However, this only helps for the builtin serde_json::Value (or the json!{} macro). If you are serializing a custom struct (which you should prefer, since it makes it easier to document what your responses should look like), you will need to switch away from a HashMap or BTreeMap, since these data types will change the order of inserted elements. I would recommend using IndexMap, from the indexmap crate, which is a map data structure that preserves insertion order.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
request Request for new functionality
Projects
None yet
Development

No branches or pull requests

2 participants