serde

How to deserialize a subfield of a struct from the original struct's JSON with Serde?

╄→尐↘猪︶ㄣ 提交于 2019-12-31 03:16:25
问题 I want to have the Test::team_size attribute be deserialized from the data of Test object itself: #[derive(Debug, Serialize, Deserialize)] struct TeamSize { pub min: i64, pub max: i64, } #[derive(Debug, Serialize, Deserialize)] struct Test { pub i: i64, pub team_size: TeamSize, } fn main() { let t: Test = serde_json::from_str(r#"{"i": -2, "min": 2, "max": 5}"#).unwrap(); assert_eq!(t.i, -2); assert_eq!(t.team_size.min, 2); assert_eq!(t.team_size.max, 5); } This code does not compile and I don

Failed to parse XML with an optional element with serde-xml-rs

断了今生、忘了曾经 提交于 2019-12-30 18:53:27
问题 I have a tree of serde-annotated structs and it succeeds in parsing the sample XML, including this fragment: <bmsg> <cmsg> <!-- ... --> <cmsg> <bmsg> Now I am testing with a large sample XML file and the following structs fail because sometimes <cmsg>..</cmsg> is missing. I was deserializing this using: #[derive(Serialize,Deserialize, Debug)] struct A { #[serde(rename="bmsg")] messages: B, // <==== } #[derive(Serialize,Deserialize, Debug)] struct B { // bmsg #[serde(rename="cmsg")] list: Vec

How can deserialization of polymorphic trait objects be added in Rust if at all?

非 Y 不嫁゛ 提交于 2019-12-28 04:19:28
问题 I'm trying to solve the problem of serializing and deserializing Box<SomeTrait> . I know that in the case of a closed type hierarchy, the recommended way is to use an enum and there are no issues with their serialization, but in my case using enums is an inappropriate solution. At first I tried to use Serde as it is the de-facto Rust serialization mechanism. Serde is capable of serializing Box<X> but not in the case when X is a trait. The Serialize trait can’t be implemented for trait objects

How can I distinguish between a deserialized field that is missing and one that is null?

拟墨画扇 提交于 2019-12-24 07:47:48
问题 I'd like to use Serde to parse some JSON as part of a HTTP PATCH request. Since PATCH requests don't pass the entire object, only the relevant data to update, I need the ability to tell between a value that was not passed, a value that was explicitly set to null , and a value that is present. I have a value object with multiple nullable fields: struct Resource { a: Option<i32>, b: Option<i32>, c: Option<i32>, } If the client submits JSON like this: {"a": 42, "b": null} I'd like to change a to

Why is a trait not implemented for a type that clearly has it implemented?

不问归期 提交于 2019-12-24 07:16:31
问题 I'm trying to use Diesel to query a MySQL database and display the results with a Handlebars template with Rocket. I have this in models.rs #[derive(Queryable, Serialize)] pub struct Post { pub id: i32, pub title: String, pub text: String, pub published: bool, } cargo run outputs this: --> src/main.rs:69:5 | 69 | Template::render("index", &results) | ^^^^^^^^^^^^^^^^ the trait `serde::ser::Serialize` is not implemented for `tasty::models::Post` | = note: required because of the requirements

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 to write a trait bound for a reference to an associated type on the trait itself?

两盒软妹~` 提交于 2019-12-18 09:08:21
问题 I have this code: extern crate serde; use serde::de::DeserializeOwned; use serde::Serialize; trait Bar<'a, T: 'a> where T: Serialize, &'a T: DeserializeOwned, { } I would like to write this using an associated type, because the type T is unimportant to the users of this type. I got this far: trait Bar { type T: Serialize; } I cannot figure out how to specify the other bound. Ultimately, I want to use a function like this: extern crate serde_json; fn test<I: Bar>(t: I::T) -> String { serde

Is it possible to flatten sub-object fields while parsing with serde_json?

谁说我不能喝 提交于 2019-12-17 21:30:42
问题 #[serde(rename)] seems to be the right option, but the documentation does not state if it is possible or how to do it. This JSON object: { "name" : "myobject" "info" : { "counter" : "3" "foo" : "bar" } } The corresponding flat Rust struct should be: #[derive(Deserialize)] struct Object { name: String, #[serde(rename="info.counter")] // wrong syntax here !! count: i32, #[serde(rename="info::foo")] // neither this works foo: String, } 回答1: There is no built-in way to do this with attributes,

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:

Why is a trait not implemented for a type that clearly has it implemented?

不打扰是莪最后的温柔 提交于 2019-12-16 20:01:37
问题 I'm trying to use Diesel to query a MySQL database and display the results with a Handlebars template with Rocket. I have this in models.rs #[derive(Queryable, Serialize)] pub struct Post { pub id: i32, pub title: String, pub text: String, pub published: bool, } cargo run outputs this: --> src/main.rs:69:5 | 69 | Template::render("index", &results) | ^^^^^^^^^^^^^^^^ the trait `serde::ser::Serialize` is not implemented for `tasty::models::Post` | = note: required because of the requirements