serde

How can I deserialize an enum when the case doesn't match?

ぐ巨炮叔叔 提交于 2020-06-08 19:20:25
问题 I have a JSON structure that looks like this: { "type": "suite", "event": "started", "test_count": 1 } I want to deserialize into these structs: #[derive(Debug, Deserialize)] enum ResultType { Suite, Test, } #[derive(Debug, Deserialize)] enum ResultEvent { Started, Failed, Ok, } #[derive(Debug, Deserialize)] struct JsonResult { #[serde(rename(deserialize = "type"))] test_type: ResultType, event: ResultEvent, test_count: Option<u32>, } I can't find a way to make serde_json use the correct case

How do I resolve “implementation of serde::Deserialize is not general enough” with actix-web's Json type?

跟風遠走 提交于 2020-04-12 19:44:37
问题 I'm writing a server using actix-web: use actix_web::{post, web, Responder}; use serde::Deserialize; #[derive(Deserialize)] struct UserModel<'a, 'b> { username: &'a str, password: &'b str, } #[post("/")] pub fn register(user_model: web::Json<UserModel>) -> impl Responder {} The compiler gives this error: error: implementation of `user::_IMPL_DESERIALIZE_FOR_UserModel::_serde::Deserialize` is not general enough --> src/user.rs:31:1 | 31 | #[post("/")] | ^^^^^^^^^^^^ | = note: `user::UserModel<

Why does Serde not support Rc and Arc types by default?

▼魔方 西西 提交于 2020-03-22 08:26:33
问题 Please explain the Serde rc feature Opt into impls for Rc<T> and Arc<T> . Serializing and deserializing these types does not preserve identity and may result in multiple copies of the same data. Be sure that this is what you want before enabling this feature. Serializing a data structure containing reference-counted pointers will serialize a copy of the inner value of the pointer each time a pointer is referenced within the data structure. Serialization will not attempt to deduplicate these

Why does Serde not support Rc and Arc types by default?

左心房为你撑大大i 提交于 2020-03-22 08:26:27
问题 Please explain the Serde rc feature Opt into impls for Rc<T> and Arc<T> . Serializing and deserializing these types does not preserve identity and may result in multiple copies of the same data. Be sure that this is what you want before enabling this feature. Serializing a data structure containing reference-counted pointers will serialize a copy of the inner value of the pointer each time a pointer is referenced within the data structure. Serialization will not attempt to deduplicate these

How to deserialize a JSON file which contains null values using Serde?

给你一囗甜甜゛ 提交于 2020-03-13 06:21:26
问题 I want to deserialize the chemical elements JSON file from Bowserinator on github using Serde. For this I created a structure with all the needed fields and derived the needed macros: #[derive(Serialize, Deserialize, Debug, Clone)] pub struct Element { name: String, appearance: String, atomic_mass: f64, boil: f64, category: String, #[serde(default)] color: String, density: f64, discovered_by: String, melt: f64, #[serde(default)] molar_heat: f64, named_by: String, number: String, period: u32,

Cannot resolve T: serde::Deserialize<'a> when deriving Deserialize on a generic struct

折月煮酒 提交于 2020-02-21 21:55:52
问题 I'm trying to write a struct that derives serde::Deserialize but it also has a field that should derive serde::Deserialize : extern crate serde; #[macro_use] extern crate serde_derive; use serde::{Deserialize, Serialize}; #[derive(PartialEq, Serialize, Deserialize)] pub struct Record<'a, T> where T: 'a + Serialize + Deserialize<'a>, { id: &'a str, created_at: &'a str, created_by: Option<&'a str>, last_updated_at: Option<&'a str>, object: &'a T, } impl<'a, T> Record<'a, T> where T: 'a +

How can I use Serde's custom (de)serialization to update a subset of arbitrary input?

梦想的初衷 提交于 2020-01-24 15:28:26
问题 I need to update specific fields of an arbitrary input file without touching any keys or values that my program does not know about. Here is an example input file: { "alpha": { "a": 1, "z": 2 }, "beta": "b" } I'd like to update alpha.a by 100: { "alpha": { "a": 101, "z": 2 }, "beta": "b" } It is possible to do this with types like serde_json::Value and toml::value::Value, but this code is very cumbersome: extern crate serde; // 1.0.66 extern crate serde_json; // 1.0.21 use serde_json::Value;

Deserialize a Vec<Foobar<T>> as Vec<T> directly when Foobar has exactly one field

不打扰是莪最后的温柔 提交于 2020-01-06 07:01:43
问题 I'm given a data-format that includes a sequence of objects with exactly one named field value each. Can I remove this layer of indirection while deserializing? When deserializing, the natural representation would be /// Each record has it's own `{ value: ... }` object #[derive(serde::Deserialize)] struct Foobar<T> { value: T, } /// The naive representation, via `Foobar`... #[derive(serde::Deserialize)] struct FoobarContainer { values: Vec<Foobar<T>>, } While Foobar adds no extra cost beyond

Deserialize a Vec<Foobar<T>> as Vec<T> directly when Foobar has exactly one field

杀马特。学长 韩版系。学妹 提交于 2020-01-06 07:01:14
问题 I'm given a data-format that includes a sequence of objects with exactly one named field value each. Can I remove this layer of indirection while deserializing? When deserializing, the natural representation would be /// Each record has it's own `{ value: ... }` object #[derive(serde::Deserialize)] struct Foobar<T> { value: T, } /// The naive representation, via `Foobar`... #[derive(serde::Deserialize)] struct FoobarContainer { values: Vec<Foobar<T>>, } While Foobar adds no extra cost beyond

How do I map a C struct with padding over 32 bytes using serde and bincode?

那年仲夏 提交于 2020-01-04 03:18:26
问题 I'm mapping a binary structure using serde and bincode. #[macro_use] extern crate serde_derive; extern crate serde; extern crate bincode; #[derive(Serialize, Deserialize)] struct Superblock { magic: [u8; 16], //reserved: [u8; 492], crc: u32, } Things work as expected, but I can't map the reserved field. Apparently fixed size arrays are only defined for sizes up to 32 bytes. How do I register my custom-sized array so that the padding gets deserialised? Is serde+bincode the right approach? I