Skip to content

Impl SerJson for HashMap<&str, T> and other types - #138

Open
Ameobea wants to merge 3 commits into
not-fl3:masterfrom
Ameobea:json-serialize-hashmap-str-key-fix
Open

Impl SerJson for HashMap<&str, T> and other types#138
Ameobea wants to merge 3 commits into
not-fl3:masterfrom
Ameobea:json-serialize-hashmap-str-key-fix

Conversation

@Ameobea

@Ameobea Ameobea commented Jun 11, 2025

Copy link
Copy Markdown
Contributor

I was integrating nanoserde in a project of mine but ran into difficulty when trying to serialize a HashMap<&'static str, MyStructThatImplsSerJson> to JSON. This was due to the fact that str implements SerJson but not &str.

This fix makes it so that things work as expected for that case and many others too (Vec<&str>, &[&usize], etc.)`

Changes

  • Add an auto-impl for SerJson for &T: SerJson. This allows types like HashMap<&'static str, &'static str> and many others to serialize to JSON while they previously couldn't
  • Add a test to verify that HashMap<&str, T> can actually be serialized and produces correct results

 * Add an auto-impl for `SerJson` for `&T: SerJson`.  This allows types like `HashMap<&'static str, &'static str>` and many others to serialize to JSON while they previously couldn't
 * Add a test to verify that `HashMap<&str, T>` can actually be serialized and produces correct results
@knickish

Copy link
Copy Markdown
Collaborator

@not-fl3 do you have any opinion on this?

@TudbuT

TudbuT commented Apr 9, 2026

Copy link
Copy Markdown
Contributor

Why is this in limbo?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants