Closed diogoaurelio closed 12 months ago
Your example works for me, after adding use serde::Serialize;
and wrapping the code starting at let
in fn main()
. Are you perhaps using multiple workspaces where the features are not the same in all instances?
Also, you don't need use indexmap::map::serde_seq;
if you're also writing that full path in the serde(with = ...)
, so you could either remove that use
or change to serde(with = "serde_seq")
. (But that's just an "unused" warning as-is.)
The serde_seq
mode will serialize as a list of key-value pairs, like:
{"data":[["banana","1"],["apple","3"]]}
If you want it to look like a JSON object with order, there's a serde_json
feature "preserve_order"
for that, which even uses IndexMap
itself! Then without the serde(with)
annotation, the default serialization output in-order looks like:
{"data":{"banana":"1","apple":"3"}}
Actually, I think that "preserve_order"
feature only matters for serde_json::Map
as found in its generic Value
representation. When you serialize directly to_string
, it seems to preserve the order regardless.
Feel free to reopen if you have further questions!
Hi, rust noob here (couple of hours under my belt), trying to serialize a map with the same order as insertion, but unable to do so:
Here the sample code:
My cargo:
From this documentation, I understood that I should be able to serialize with order.
Many thanks in advance.