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
23
votes
3 answers

How do I use Serde to (de)serialize arrays greater than 32 elements, such as [u8; 128]?

I have a struct containing a byte array that I would like to serialize and deserialize to and from binary, but it only works for arrays up to 32 elements. Here is my minimal example code main.rs: #[macro_use] extern crate serde_derive; extern crate…
Tobias Hermann
  • 9,936
  • 6
  • 61
  • 134
23
votes
1 answer

"invalid type: map, expected a sequence" when deserializing a nested JSON structure with Serde

I am trying to poll the GitHub API for issues and print them out. To do so, I need to deserialize a nested JSON structure that I receive from a cURL GET request. I am trying to get the url for all the objects in the items array: { "total_count":…
zep
  • 271
  • 1
  • 3
  • 8
22
votes
1 answer

How to pass an array of objects to WebAssembly and convert it to a vector of structs with wasm-bindgen?

It's possible to pass an array of integers like this: const js = import("./webassembly_rust"); let array_nums = [1,2,3,4,5,6,7,8,9]; js.then(js => { js.test( array_nums ); }); to WebAssembly and save it in a vector like this: extern crate…
Gregor
  • 331
  • 2
  • 6
22
votes
1 answer

Rust & Serde JSON deserialization examples?

I'm trying to figure out how to deserialize JSON into a structure using Serde. For instance, the example JSON on serde_json's own documentation contains the following data: { "FirstName": "John", "LastName": "Doe", "Age": 43, …
vegai
  • 330
  • 1
  • 3
  • 6
20
votes
1 answer

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

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:…
thesdev
  • 798
  • 9
  • 15
19
votes
3 answers

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

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> } #[derive(Serialize)] struct Section { header:…
snowbane
  • 245
  • 1
  • 3
  • 10
19
votes
3 answers

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

I'm trying to solve the problem of serializing and deserializing Box. 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…
Dmitry Gordon
  • 2,229
  • 12
  • 20
18
votes
2 answers

How to decode JSON object with Rust keyword attribute name?

I was wondering if it is possible to decode a JSON object in Rust that has an attribute name which is also a Rust keyword. I am working with the rustc-serialize crate and my struct definition looks like this: #[derive(RustcDecodable)] struct MyObj…
rking788
  • 431
  • 1
  • 5
  • 12
17
votes
3 answers

How to unit-test a deserialization function used in serde(deserialize_with)?

I have a struct which implements Deserialize and uses the serde(deserialize_with) on a field: #[derive(Debug, Deserialize)] struct Record { name: String, #[serde(deserialize_with = "deserialize_numeric_bool")] is_active: bool, } The…
pixunil
  • 641
  • 1
  • 5
  • 19
16
votes
3 answers

Convert two types into a single type with Serde

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…
XAMPPRocky
  • 3,160
  • 5
  • 25
  • 45
15
votes
4 answers

Using `serde::Serialize` with `Option`

When trying to serialize Option> I'm encountering an error: error[E0308]: mismatched types --> src/main.rs:39:14 | 39 | #[derive(Serialize, Debug)] | ^^^^^^^^^ expected struct `DateTime`, found enum…
Jonathan Woollett-light
  • 2,813
  • 5
  • 30
  • 58
15
votes
1 answer

Rust Diesel: the trait bound `NaiveDateTime: Deserialize<'_>` is not satisfied

I am new to rust and diesel. And trying to create a small demo api using rocket framework. Getting error: the trait bound NaiveDateTime: Deserialize<'_> is not satisfied I googled and found some useful links like here :…
Shaksham Singh
  • 491
  • 1
  • 5
  • 19
15
votes
2 answers

How to handle potentially missing fields using serde_json and the Value enum?

I have a data stream of JSON where some JSON objects may be missing certain fields or have fields I am unaware of in advance. My solution is to use: let v: Value = serde_json::from_str(data)?; How do I handle accessing the field stuff? If I know…
James Smith
  • 249
  • 1
  • 2
  • 8
15
votes
2 answers

How to use a custom serde deserializer for chrono timestamps?

I'm trying to parse JSON into a struct which has a chrono::DateTime field. The JSON has the timestamps saved in a custom format that I wrote a deserializer for. How do I connect the two and get it working using #[serde(deserialize_with)]? I'm using…
Emre
  • 503
  • 4
  • 7
14
votes
1 answer

How can I use serde to serialize a struct to another Rust data structure?

I have a data structure Document, which I would like to serialize other Rust structs to. It is basically a HashMap for the fields internally, however it interacts with a database API, so I will definitely want to convert other types into those…
stimulate
  • 1,199
  • 1
  • 11
  • 30
1
2
3
55 56