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
0
votes
0 answers

How do I implement Serialize and Deserialize if I already have conversions to and from serde-json Value?

Serialization and deserialization for most of my structs and enums can be easily implemented using #[derive(Serialize, Deserialize)]. A few structs require careful crafting to implement. I am new to serde. I succeeded in implementing the From trait…
Paul Chernoch
  • 5,275
  • 3
  • 52
  • 73
0
votes
1 answer

How do I modify the JSON output for a Result serialized with serde?

A simple code: use serde::Serialize; #[derive(Serialize)] struct MyStruct { foo: Result } fn main() { let m = MyStruct { foo: Ok(43) }; let n = MyStruct { foo: Err("oh no!".into()) }; …
jeanpaul62
  • 9,451
  • 13
  • 54
  • 94
0
votes
1 answer

Parsing file returns error "thread 'main' panicked"

I'm struggling with serde-xml-rs and hope someone can help me out. I've been able to parse a single "pet" from the sample XML below. But, when I try to parse a file with two "pet" entries under "pets", as illustrated below, I get the error "thread…
ghoetker
  • 161
  • 1
  • 9
0
votes
1 answer

Can't use locally created vector because "borrowed"

I can't get first element of the vector because of the error and can't change struct design either. I tried borrowing but struct expects a ExtrudeGeometry. #[wasm_bindgen] pub fn toCollection(arr: js_sys::Array, r_type: String) -> JsValue { let…
mertselimb
  • 167
  • 7
0
votes
3 answers

How to efficiently extract a portion of JSON as a Vec without intermediate structs?

I have JSON content where, deeply nested, there is an array of numbers I want to extract. I'd like to not create the intermediate structs, so I tried the following: ... get f let json = serde_json::from_reader::<_, serde_json::Value>(f)?; let xs:…
Listerone
  • 1,381
  • 1
  • 11
  • 25
0
votes
1 answer

De-serializing request and response with rust serde

I am trying to de/serialize request and response on server/client side. Below is my what I have tried :- use std::collections::HashMap; use std::fmt; use std::io::prelude::*; use std::net::TcpStream; use serde_derive::{Deserialize,…
ravi
  • 10,994
  • 1
  • 18
  • 36
0
votes
1 answer

Deserialize XML with multiple schema structures

How can I deserialize an XML-string (using serde derive) returned by an API that might have different children? If I call an API it might return one of the following result sets: use serde::Deserialize; use serde_xml_rs::from_str; #[derive(Debug,…
rideron89
  • 491
  • 3
  • 14
0
votes
0 answers

How do I generate a TOML file with placeholder lines showing all missing values using Serde?

I want the Serde serializer to write empty values to the TOML output. The result is a configuration file so it would be great if I could include empty keys so the user sees that the option is available. struct X { attr: Option, } should…
Bomgar
  • 553
  • 1
  • 3
  • 13
0
votes
0 answers

How to express this TOML format in Serde

I have a Rust structure that I can deserialize: pub struct ProcessReference { pub alias: Name, pub source: String, #[serde(rename = "input")] pub initializations: Option>, } where input is optional. This…
Andrew Mackenzie
  • 5,477
  • 5
  • 48
  • 70
0
votes
1 answer

How to deserialize XML to different structs based on tag name using serde_xml_rs?

I am using serde-xml-rs for easy information transfer that I or anyone else can change later without having to code in Rust. It looks something like this: 1
Andrew Chon
  • 103
  • 1
  • 1
  • 5
0
votes
1 answer

Is it possible to deserialize data that looks like JSON (but isn't) using serde_json?

I have a hard time deserializing (using Rust's serde and serde_json v1.0) the following JSON I receive: { ["string content"] } The object's array is not identified by a key, so the following doesn't work: #[derive(Deserialize)] struct Data { …
big_gie
  • 2,829
  • 3
  • 31
  • 45
0
votes
0 answers

Rust serde_json serializer for pretty print of [u8; 32]

I want to get the pretty print in Rust for [u8; 32], with serde_json, but can't make it work. I know if the following struct is a (Vec), that works, as this playground code. But I have to keep the ([u8; 32]) struct because it's everywhere in an…
gary
  • 1,569
  • 3
  • 20
  • 30
0
votes
0 answers

How to use EnumAccess in Serde?

I am deserializing a externally coded enum for Avro. I have created an enum which matches it schema and I don't know how to take the variant identifer (a number) and choose the proper enum variant. The target struct: #[derive(Deserialize,…
xrl
  • 2,155
  • 5
  • 26
  • 40
0
votes
1 answer

How to add an additional data point to an instance of a struct when writing it to a file with Serde?

A Person has a first_name and a last_name. How do I add a time of export to a CSV when writing an instance of Person to a CSV? #[macro_use] extern crate serde_derive; extern crate csv; extern crate serde; use std::time::{SystemTime,…
Greg
  • 8,175
  • 16
  • 72
  • 125
0
votes
0 answers

Cannot deserialize with Serde: the `?` operator can only be used in a function that returns `Result`

I'm trying to deserialize a json string to a struct but I'm having trouble with getting it to work. All I want to do is print out the status and or result of the first one to match run["meta"]["username"] against a given username. However despite…
Thermatix
  • 2,757
  • 21
  • 51