Inconsistent JSON output when serializing enums in Rust

Advertisements I’m using serde_json to serialize an enum to JSON. This is what I’m getting when serializing std::net::IpAddr: use std::net::IpAddr; fn main() { let addr : IpAddr = "127.0.0.1".parse().unwrap(); let json = serde_json::to_string(&addr).unwrap(); println!("{}", json); } output: "127.0.0.1" However, when I write my own similar enum and serialize it, it looks different: use serde::{Deserialize, Serialize};… Read More Inconsistent JSON output when serializing enums in Rust

How to re-use existing, derived deserialize implementation when deserializing some variants of an enum?

Advertisements I have an enum that contains variants which implement ser/de via derive, and ones which do not. I would like to implement Deserialize in such a way that I do not have to match every possible sub-variant, e.g. re-use the existing, derived Deserialize implementation. Here’s the example: enum GlobalError { MyErrorVariant(MyError), // Implements ser/de… Read More How to re-use existing, derived deserialize implementation when deserializing some variants of an enum?

How to return an error from `deserialize`?

Advertisements When implementing Deserialize, how to return an error? impl<‘de> Deserialize<‘de> for Response { fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: Deserializer<‘de> { … // Which error to return here? } } Here every error must be convertible to D::Error, but D::Error maybe any whatsoever type. So, I cannot create a type convertible to… Read More How to return an error from `deserialize`?

Serde deserialize String into u64

Advertisements I can’t find how to deserialize a JSON string into an integer type using serde. My attempt won’t compile: #[derive(Debug, Deserialize)] struct WebsocketMessage { #[serde(deserialize_with = "str::parse")] timestamp: u64 } >Solution : Well, str::parse simply doesn’t have the right type. The type must be fn<‘de, D>(D) -> Result<T, D::Error> where D: Deserializer<‘de> as documented… Read More Serde deserialize String into u64

Rust Deserialization Lifetimes Problem : 'de must outlive 'a

Advertisements I have the following two structs for which I derive serde traits. use serde::{Serialize, Deserialize}; #[derive(Serialize, Deserialize)] pub struct Item<‘a> { pub id: &’a str, pub name: &’a str } #[derive(Serialize, Deserialize)] struct Set<‘a> { items: Vec<Item<‘a>> } When I try to compile this, I am getting am getting the following error message to… Read More Rust Deserialization Lifetimes Problem : 'de must outlive 'a

Unable to parse graphql endpoint

Advertisements I am trying to parse the response to a graphql endpoint using this code: extern crate serde_derive; use gql_client::Client; use serde::{Deserialize, Serialize}; #[derive(Debug, Serialize, Deserialize)] pub struct DevActivity { pub data: Data, } #[derive(Debug, Serialize, Deserialize)] pub struct Data { #[serde(rename = "getMetric")] pub get_metric: GetMetric, } #[derive(Debug, Serialize, Deserialize)] pub struct GetMetric {… Read More Unable to parse graphql endpoint