#iterator #serde-json #json-key #string-key #serialize-deserialize #key #hash-map

serde_json_any_key

Workaround for "key must be a string" error with serde_json. De/serialize any HashMap<K,V>, Vec<K,V>, Iter<(&K,&V)>, or Iter<&(K,V)> as a JSON map.

4 stable releases

2.0.0 Jun 13, 2022
1.1.0 Jun 3, 2022
1.0.1 Jun 2, 2022

#658 in Encoding

Download history 2751/week @ 2024-07-19 2687/week @ 2024-07-26 3506/week @ 2024-08-02 5254/week @ 2024-08-09 2554/week @ 2024-08-16 1360/week @ 2024-08-23 2991/week @ 2024-08-30 2405/week @ 2024-09-06 3463/week @ 2024-09-13 2555/week @ 2024-09-20 2618/week @ 2024-09-27 2194/week @ 2024-10-04 3097/week @ 2024-10-11 3539/week @ 2024-10-18 2673/week @ 2024-10-25 3504/week @ 2024-11-01

13,202 downloads per month
Used in 2 crates

Unlicense

49KB
765 lines

serde_json_any_key

View the docs on docs.rs

Workaround for "key must be a string" error with serde_json. De/serialize any HashMap<K,V>, Vec<(K,V)>, Iter<(&K,&V)>, or Iter<&(K,V)> as a JSON map.

The output will be the same as if you manually serialized K to a String. If K already is a String, it will behave identically to serde_json.

Serialization is implemented for any type that implements IntoIterator<Item=(K,V)>, IntoIterator<Item=&(K,V)>, or IntoIterator<Item=(&K,&V)>.
Deserialization is implemented for any type that implements FromIterator<(K,V)>.

De/serialization of structs with nested maps is supported via the following attributes:
#[serde(with = "any_key_vec")]
#[serde(with = "any_key_map")]

This crate is implemented purely using safe, stable Rust.

Example

use std::collections::HashMap;
use serde::{Serialize, Deserialize};
use serde_json_any_key::*;

#[derive(Clone, Copy, Deserialize, Serialize, PartialEq, Eq, Hash, Debug)]
pub struct Test {
  pub a: i32,
  pub b: i32
}

fn main() {
 // Create a map with a struct key
 let mut map = HashMap::<Test, Test>::new();
 map.insert(Test {a: 3, b: 5}, Test {a: 7, b: 9});
 
 // Regular serde_json cannot serialize this map
 let fail = serde_json::to_string(&map);
 assert_eq!(fail.err().unwrap().to_string(), "key must be a string");
 
 // Use this crate's utility function
 // Outputs {"{\"a\":3,\"b\":5}":{"a":7,"b":9}}
 let ser1 = map.to_json_map().unwrap();
 assert_eq!(ser1, r#"{"{\"a\":3,\"b\":5}":{"a":7,"b":9}}"#);
 
 // You can also serialize a Vec or slice of tuples to a JSON map
 let mut vec = Vec::<(Test, Test)>::new();
 vec.push((Test {a: 3, b: 5}, Test {a: 7, b: 9}));
 let ser2 = vec.to_json_map().unwrap();

 // Output is identical in either case
 assert_eq!(ser1, ser2);
 
 // And can be deserialized to either type
 let deser_map: HashMap<Test, Test> = json_to_map(&ser2).unwrap();
 let deser_vec: Vec<(Test, Test)> = json_to_vec(&ser1).unwrap();
 assert_eq!(map, deser_map);
 assert_eq!(vec, deser_vec);
 
 // De/serialization of structs with nested maps is supported via the following attributes:
 // #[serde(with = "any_key_vec")]
 // #[serde(with = "any_key_map")]
 
 // Both the "map" and "vec" fields will serialize identically - as a JSON map
 #[derive(Clone, Deserialize, Serialize, PartialEq, Eq, Debug)]
 pub struct NestedTest {
   #[serde(with = "any_key_map")]
   map: HashMap<Test, Test>,
   #[serde(with = "any_key_vec")]
   vec: Vec<(Test, Test)>
 }
 let nested = NestedTest {
   map: map,
   vec: vec,
 };
 // You can use the usual serde_json functions now
 let ser_nested = serde_json::to_string(&nested).unwrap();
 let deser_nested: NestedTest = serde_json::from_str(&ser_nested).unwrap();
 assert_eq!(nested, deser_nested);
}

Dependencies

~0.7–1.6MB
~34K SLoC