serde

Deserializing TOML into vector of enum with values

人走茶凉 提交于 2019-12-01 08:13:39
I'm trying to read a TOML file to create a struct that contains a vector of enums with associated values. Here's the sample code: extern crate serde; #[macro_use] extern crate serde_derive; extern crate toml; use std::fs::File; use std::io::Read; #[derive(Debug, Deserialize, PartialEq)] struct Actor { name: String, actions: Vec<Actions>, } #[derive(Debug, Deserialize, PartialEq)] enum Actions { Wait(usize), Move { x: usize, y: usize }, } fn main() { let input_file = "./sample_actor.toml"; let mut file = File::open(input_file).unwrap(); let mut file_content = String::new(); let _bytes_read =

Deserializing TOML into vector of enum with values

穿精又带淫゛_ 提交于 2019-12-01 07:42:35
问题 I'm trying to read a TOML file to create a struct that contains a vector of enums with associated values. Here's the sample code: extern crate serde; #[macro_use] extern crate serde_derive; extern crate toml; use std::fs::File; use std::io::Read; #[derive(Debug, Deserialize, PartialEq)] struct Actor { name: String, actions: Vec<Actions>, } #[derive(Debug, Deserialize, PartialEq)] enum Actions { Wait(usize), Move { x: usize, y: usize }, } fn main() { let input_file = "./sample_actor.toml"; let

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,

How to implement `serde::Serialize` for a boxed trait object?

给你一囗甜甜゛ 提交于 2019-12-01 01:44:03
问题 I ran into a problem trying to create a generic vector for a struct. This was my first attempt: #[derive(Serialize)] struct Card { sections: Vec<Section<WidgetTrait>> } #[derive(Serialize)] struct Section<T: WidgetTrait> { header: String, widgets: Vec<T> } This has brought me to an error that Sized is not implemented and WidgetTrait size is not known at compile time. My next attempt was to use Box<WidgetTrait> like so: #[derive(Serialize)] struct Section { header: String, widgets: Vec<Box

How can I deserialize an optional field with custom functions using Serde?

牧云@^-^@ 提交于 2019-11-30 11:19:42
I want to serialize and deserialize a chrono::NaiveDate with custom functions, but the Serde book does not cover this functionality and the code docs also do not help. #[macro_use] extern crate serde_derive; extern crate serde; extern crate serde_json; extern crate chrono; use chrono::NaiveDate; mod date_serde { use chrono::NaiveDate; use serde::{self, Deserialize, Serializer, Deserializer}; pub fn serialize<S>(date: &Option<NaiveDate>, s: S) -> Result<S::Ok, S::Error> where S: Serializer { if let Some(ref d) = *date { return s.serialize_str(&d.format("%Y-%m-%d").to_string()) } s.serialize

How do I deserialize into trait, not a concrete type?

杀马特。学长 韩版系。学妹 提交于 2019-11-30 08:21:24
问题 I'm trying to do struct serialization, in which the bytes would eventually be sent down a pipe, reconstructed and methods be called on them. I created a trait these structs would implement as appropriate and I'm using serde and serde-cbor for serialization: extern crate serde_cbor; #[macro_use] extern crate serde_derive; extern crate serde; use serde_cbor::ser::*; use serde_cbor::de::*; trait Contract { fn do_something(&self); } #[derive(Debug, Serialize, Deserialize)] struct Foo { x: u32, y:

Convert two types into a single type with Serde

不问归期 提交于 2019-11-30 04:55:15
问题 I'm writing for a program that hooks into a web service which sends back JSON. When a certain property isn't there it provides a empty object, with all its fields as empty strings instead of excluding the value. When the property exists, some of the properties are u64 . How can I have it so Serde handles this case? Rust Structs #[derive(Clone, Debug, Deserialize)] struct WebResponse { foo: Vec<Foo>, } #[derive(Clone, Debug, Deserialize)] struct Foo { points: Points, } #[derive(Clone, Debug,

How to implement `serde::Serialize` for a boxed trait object?

帅比萌擦擦* 提交于 2019-11-29 10:50:07
I ran into a problem trying to create a generic vector for a struct. This was my first attempt: #[derive(Serialize)] struct Card { sections: Vec<Section<WidgetTrait>> } #[derive(Serialize)] struct Section<T: WidgetTrait> { header: String, widgets: Vec<T> } This has brought me to an error that Sized is not implemented and WidgetTrait size is not known at compile time. My next attempt was to use Box<WidgetTrait> like so: #[derive(Serialize)] struct Section { header: String, widgets: Vec<Box<WidgetTrait>> } Playground This has led me to an error: error[E0277]: the trait bound `WidgetTrait: serde:

How do I deserialize into trait, not a concrete type?

主宰稳场 提交于 2019-11-29 06:27:02
I'm trying to do struct serialization, in which the bytes would eventually be sent down a pipe, reconstructed and methods be called on them. I created a trait these structs would implement as appropriate and I'm using serde and serde-cbor for serialization: extern crate serde_cbor; #[macro_use] extern crate serde_derive; extern crate serde; use serde_cbor::ser::*; use serde_cbor::de::*; trait Contract { fn do_something(&self); } #[derive(Debug, Serialize, Deserialize)] struct Foo { x: u32, y: u32, } #[derive(Debug, Serialize, Deserialize)] struct Bar { data: Vec<Foo>, } #[derive(Debug,