serde-json

How do I use Serde to serialize a HashMap with structs as keys to JSON?

牧云@^-^@ 提交于 2019-12-22 05:29:27
问题 I want to serialize a HashMap with structs as keys: extern crate serde_json; // 1.0.22 #[macro_use] extern crate serde_derive; // 1.0.68 use std::collections::HashMap; fn main() { #[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Hash)] struct Foo { x: u64, } #[derive(Serialize, Deserialize, Debug)] struct Bar { x: HashMap<Foo, f64>, } let mut p = Bar { x: HashMap::new() }; p.x.insert(Foo { x: 0 }, 0.0); let serialized = serde_json::to_string(&p).unwrap(); } This code compiles, but when

How do I serialize an enum without including the name of the enum variant?

走远了吗. 提交于 2019-12-17 10:06:01
问题 I am trying to serialize an enum to a JSON string. I implemented Serialize trait for my enum as it is described in the docs, but I always get {"offset":{"Int":0}} instead of the desired {"offset":0} . extern crate serde; extern crate serde_json; use std::collections::HashMap; use serde::ser::{Serialize, Serializer}; #[derive(Debug)] enum TValue<'a> { String(&'a str), Int(&'a i32), } impl<'a> Serialize for TValue<'a> { fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S:

How do I change Serde's default implementation to return an empty object instead of null?

北城余情 提交于 2019-12-13 16:46:23
问题 I'm developing an API wrapper and I'm having some troubles with the deserialization of an empty JSON object. The API returns this JSON object. Mind the empty object at entities : { "object": "page", "entry": [ { "id": "1158266974317788", "messaging": [ { "sender": { "id": "some_id" }, "recipient": { "id": "some_id" }, "message": { "mid": "mid.$cAARHhbMo8SBllWARvlfZBrJc3wnP", "seq": 5728, "text": "test", "nlp": { "entities": {} // <-- here } } } ] } ] } This is my equivalent struct of the

Is there is a simpler way to convert a type upon deserialization?

末鹿安然 提交于 2019-12-01 05:39:16
Using serde_json , I have JSON objects with String s that I need to convert to floats. I've stumbled upon a custom deserializer solution, but it seems like a hack. Here is a working playground example of the code below. #[macro_use] extern crate serde_derive; extern crate serde; extern crate serde_json; use serde_json::Error; use serde::de::{Deserialize, DeserializeOwned, Deserializer}; #[derive(Serialize, Deserialize)] struct Example { #[serde(deserialize_with = "coercible")] first: f64, second: f64, } fn coercible<'de, T, D>(deserializer: D) -> Result<T, D::Error> where T: DeserializeOwned,

Is there is a simpler way to convert a type upon deserialization?

随声附和 提交于 2019-12-01 02:17:03
问题 Using serde_json, I have JSON objects with String s that I need to convert to floats. I've stumbled upon a custom deserializer solution, but it seems like a hack. Here is a working playground example of the code below. #[macro_use] extern crate serde_derive; extern crate serde; extern crate serde_json; use serde_json::Error; use serde::de::{Deserialize, DeserializeOwned, Deserializer}; #[derive(Serialize, Deserialize)] struct Example { #[serde(deserialize_with = "coercible")] first: f64,

Lifetime error when creating a function that returns a value implementing serde::Deserialize

流过昼夜 提交于 2019-11-26 19:11:25
I'm using serde and serde_json 1.0 to decode data from a base64 string: fn from_base64_str<T: Deserialize>(string: &str) -> T { let slice = decode_config(string, URL_SAFE).unwrap(); serde_json::from_slice(&slice).unwrap() } When I compile, I got this: error[E0106]: missing lifetime specifier --> src/main.rs:6:23 | 6 | fn from_base64_str<T: Deserialize>(string: &str) -> T { | ^^^^^^^^^^^ expected lifetime parameter Checking the serde doc, Deserialize is defined as: pub trait Deserialize<'de>: Sized { So I added the lifetime: fn from_base64_str<'de, T: Deserialize<'de>>(string: &str) -> T { let

Lifetime error when creating a function that returns a value implementing serde::Deserialize

一笑奈何 提交于 2019-11-26 06:49:23
问题 I\'m using serde and serde_json 1.0 to decode data from a base64 string: fn from_base64_str<T: Deserialize>(string: &str) -> T { let slice = decode_config(string, URL_SAFE).unwrap(); serde_json::from_slice(&slice).unwrap() } When I compile, I got this: error[E0106]: missing lifetime specifier --> src/main.rs:6:23 | 6 | fn from_base64_str<T: Deserialize>(string: &str) -> T { | ^^^^^^^^^^^ expected lifetime parameter Checking the serde doc, Deserialize is defined as: pub trait Deserialize<\'de>