serde

How to deserialize “NaN” as `nan` with serde_json?

孤街醉人 提交于 2019-12-05 22:08:01
I have datatypes which look like this: #[derive(Serialize, Deserialize, Debug)] #[serde(rename_all = "camelCase")] pub struct Matrix { #[serde(rename = "numColumns")] pub num_cols: usize, #[serde(rename = "numRows")] pub num_rows: usize, pub data: Vec<f64>, } My JSON bodies look something like this: { "numRows": 2, "numColumns": 1, "data": [1.0, "NaN"] } This is the serialization provided by Jackson (from a Java server we use), and is valid JSON. Unfortunately if we call serde_json::from_str(&blob) we get an error: Error("invalid type: string "NaN", expected f64", [snip] I understand there are

Generate pretty (indented) JSON with serde

筅森魡賤 提交于 2019-12-05 11:44:26
问题 Using the serde_json crate, I can use ::serde_json::to_string(&obj) to serialize an object into a JSON string. The resulting JSON uses compact formatting, like: {"foo":1,"bar":2} But how do I generate pretty/indented JSON? For example, I'd like to get this: { "foo": 1, "bar": 2 } 回答1: The serde_json::to_string_pretty function generates pretty-printed indented JSON. #[macro_use] extern crate serde_json; fn main() { let obj = json!({"foo":1,"bar":2}); println!("{}", serde_json::to_string_pretty

How do I store a result using Serde Zero-copy deserialization of a Futures-enabled Hyper Chunk?

丶灬走出姿态 提交于 2019-12-04 03:54:52
问题 I'm using futures, tokio, hyper, and serde_json to request and deserialize some data that I need to hold until my next request. My initial thought was to make a struct containing the hyper::Chunk and the deserialized data that borrows from the Chunk , but couldn't get the lifetimes right. I tried using the rental crate, but I can't get this to work either. Perhaps I'm using the 'buffer lifetime before declaring the buffer Vec , but maybe I've messed something else up: #[rental] pub struct

How do I serialize or deserialize an Arc<T> in Serde?

 ̄綄美尐妖づ 提交于 2019-12-04 00:48:22
问题 I have a struct that contains children of its own type. These children are wrapped in Arc s, and I'm getting issues when calling serde_json::to_string on it. My struct is: #[derive(Serialize, Deserialize)] pub struct Category { pub id: i32, pub name: String, pub parent_id: i32, pub children: Vec<Arc<Category>>, } This produces the error the trait 'serde::Serialize' is not implemented for 'std::sync::Arc<db::queries::categories::Category>' I've tried a few different approaches to get

Generate pretty (indented) JSON with serde

我是研究僧i 提交于 2019-12-03 23:20:37
Using the serde_json crate, I can use ::serde_json::to_string(&obj) to serialize an object into a JSON string. The resulting JSON uses compact formatting, like: {"foo":1,"bar":2} But how do I generate pretty/indented JSON? For example, I'd like to get this: { "foo": 1, "bar": 2 } The serde_json::to_string_pretty function generates pretty-printed indented JSON. #[macro_use] extern crate serde_json; fn main() { let obj = json!({"foo":1,"bar":2}); println!("{}", serde_json::to_string_pretty(&obj).unwrap()); } This approach defaults to 2 spaces of indentation, which happens to be what you asked

How can I get Serde to allocate strings from an arena during deserialization?

牧云@^-^@ 提交于 2019-12-03 22:10:55
问题 I have a struct with string fields. I'd like to control how the memory for the strings is allocated. In particular, I'd like to allocate them using something like copy_arena. Maybe I could make a custom ArenaString type, but I don't see how to get a reference to the Arena into the deserialization code, and assuming that's possible, then I'll have to deal with the arena lifetime, right? 回答1: Here is one possible implementation that uses serde::de::DeserializeSeed to expose the arena allocator

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

╄→гoц情女王★ 提交于 2019-12-01 18:18:06
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<C>, } Which resulted in an error in the second struct: panicked at 'called `Result::unwrap()` on an

Parsing JSON with multiple representation in the same attribute

丶灬走出姿态 提交于 2019-12-01 17:33:11
I'm relatively new to Rust, and even more so to Serde, so I'm having trouble finding out if this is even doable. I have a JSON file which has two different representations for the same key: "coordinates": [ [ [ 121.423364, 24.9913596 ], [ 121.4233327, 24.9912977 ], ] ] and this: "coordinates": [ [ 121.4472492, 25.0052053 ], [ 121.4466457, 25.0028547 ] ] There is a two dimensional array and a three dimensional array representing ways in the same attribute. This makes the file hard to serialize. Here is the code that I implemented: #[derive(Serialize, Deserialize, Debug)] struct Geometry { #

Parsing JSON with multiple representation in the same attribute

巧了我就是萌 提交于 2019-12-01 17:13:29
问题 I'm relatively new to Rust, and even more so to Serde, so I'm having trouble finding out if this is even doable. I have a JSON file which has two different representations for the same key: "coordinates": [ [ [ 121.423364, 24.9913596 ], [ 121.4233327, 24.9912977 ], ] ] and this: "coordinates": [ [ 121.4472492, 25.0052053 ], [ 121.4466457, 25.0028547 ] ] There is a two dimensional array and a three dimensional array representing ways in the same attribute. This makes the file hard to serialize

Why does serde_json::from_reader take ownership of the reader?

随声附和 提交于 2019-12-01 15:16:04
My code: fn request_add<T>(request: &mut Request, collection_name: &'static str) -> Fallible<Fallible<String>> where T: serde::Serialize + serde::de::DeserializeOwned, { let add_dao = dao::MongoDao::new(collection_name); let obj = serde_json::from_reader::<Body, T>(request.body)?; //cannot move out of borrowed content Ok(add_dao.add(&obj)) } I have a cannot move out of borrowed content error, because request is a reference, but why does serde_json::from_reader not use a mut reference? Why does it need ownership? And how can I fix it? Because it's an API guideline : Generic reader/writer