Questions tagged [serde]

Serde is a framework for serializing and deserializing Rust data structures efficiently and generically.

Serde is a framework for serializing and deserializing Rust data structures efficiently and generically.

Site

Repository

crates.io

839 questions
14
votes
1 answer

How can I accept multiple deserialization names for the same Serde field?

I am trying to use Serde to deserialize JSON (serde-json) and XML (serde-xml-rs) files based on the following struct: use serde_derive::Deserialize; #[derive(Debug, Clone, PartialEq, Deserialize)] pub struct SchemaConfig { pub name: String, …
MarcioPorto
  • 555
  • 7
  • 22
14
votes
4 answers

Deserialize a JSON string or array of strings into a Vec

I'm writing a crate that interfaces with a JSON web API. One endpoint usually returns responses of the form { "key": ["value1", "value2"] }, but sometimes there's only one value for the key, and the endpoint returns { "key": "value" } instead of {…
Arnavion
  • 3,627
  • 1
  • 24
  • 31
13
votes
2 answers

Deserialize file using serde_json at compile time

At the beginning of my program, I read data from a file: let file = std::fs::File::open("data/games.json").unwrap(); let data: Games = serde_json::from_reader(file).unwrap(); I would like to know how it would be possible to do this at compile time…
Nils André
  • 571
  • 5
  • 17
13
votes
1 answer

How to derive serde::Deserialize for a struct with members with lifetimes

How can I derive Deserialize for a struct with objects with different or equal lifetimes inside? playground #[derive(Default, Debug, serde::Deserialize, serde::Serialize)] struct B<'a> { b: &'a str, } #[derive(Default, Debug,…
VP.
  • 15,509
  • 17
  • 91
  • 161
13
votes
4 answers

How can I support an unknown or other value for a Serde enum?

I have a JSON API that returns an object that looks like this: { "PrivatePort": 2222, "PublicPort": 3333, "Type": "tcp" } To capture this, I have an enum and a struct: #[derive(Eq, PartialEq, Deserialize, Serialize, Debug)] #[serde(rename_all…
Travis Gockel
  • 26,877
  • 14
  • 89
  • 116
13
votes
1 answer

Serialization of large struct to disk with Serde and Bincode is slow

I have a struct that contains a vector of 2³¹ u32 values (total size about 8GB). I followed the bincode example to write it to disk: #[macro_use] extern crate serde_derive; extern crate bincode; use std::fs::File; use…
m00am
  • 5,910
  • 11
  • 53
  • 69
13
votes
4 answers

How can we write a generic function for checking Serde serialization and deserialization?

In a project where custom Serde (1.0) serialization and deserialization methods are involved, I have relied on this test routine to check whether serializing an object and back would yield an equivalent object. // let o: T = ...; let buf: Vec =…
E_net4
  • 27,810
  • 13
  • 101
  • 139
13
votes
2 answers

Is there is a simpler way to convert a type upon deserialization?

Using serde_json, I have JSON objects with Strings 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…
user1992266
  • 243
  • 2
  • 9
12
votes
1 answer

rust serde - flatten path on deserialize

I want to deserialize a pretty deep JSON to Rust struct: { "root": { "f1": { "f2": { "f3": 123 } } } } When deriving Deserialize, I will have to create too many structs - one for each level for the above JSON…
Rahul
  • 1,727
  • 3
  • 18
  • 33
12
votes
1 answer

How to pass options to Rust's serde that can be accessed in Deserialize::deserialize()?

For context: I'm writing a ray tracer in Rust but I'm struggling with finding a good way to load the scene in a filesystem-agnostic way. I'm using serde so that I don't have to invent my own file format (yet). The assets (image textures and mesh…
Max Klein
  • 468
  • 1
  • 6
  • 22
12
votes
3 answers

How to get at one particular item in JSON file using serde_json without deriving structs?

I have a complex JSON file and I'd like to extract only a single value out of it. I could define all of the structs and derive Deserialize on all of them, but I'd like to just write a little manual code to pull that one value out. The Serde…
Listerone
  • 1,381
  • 1
  • 11
  • 25
12
votes
1 answer

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

My code: fn request_add(request: &mut Request, collection_name: &'static str) -> Fallible> where T: serde::Serialize + serde::de::DeserializeOwned, { let add_dao = dao::MongoDao::new(collection_name); let obj =…
11
votes
1 answer

Why Am I Getting "Cannot Derive Macro In This Scope"?

Attempting cargo build against this code: #![allow(unused)] use serde::{Deserialize, Serialize}; use serde_json::{Result, Value}; #[derive(Serialize, Deserialize,Debug)] struct Repository{ r#type: String, url: String, } fn main() { …
Onorio Catenacci
  • 14,928
  • 14
  • 81
  • 132
11
votes
2 answers

How can I ignore extra tuple items when deserializing with Serde? ("trailing characters" error)

Serde ignores unknown named fields when deserializing into regular structs. How can I similarly ignore extra items when deserializing into tuple structs (e.g. from a heterogeneous JSON array)? For example, this code ignores the extra "c" field just…
Jo Liss
  • 30,333
  • 19
  • 121
  • 170
11
votes
1 answer

Is there a way to omit wrapper/root objects when deserializing objects with Serde?

I have the following object: { "data": { "id": 1, "name": "South America", "countries": { "data": [ { "id": 122, "name": "Brazil", "capital": "Brasilia" } ] } } } I'd…
tehAnswer
  • 960
  • 1
  • 13
  • 28
1 2
3
55 56