974 questions
3
votes
1
answer
65
views
How to implement read trait? [duplicate]
I'm trying to work through Rust and using serde_yml as opposed to serde_yaml which has caused me much great confusion, and can't seem to find examples for parsing yaml from a file. I've been running ...
1
vote
0
answers
99
views
How do I write nested custom serializers?
I am trying to write custom deserialisers for a multi-level data structure (for serde in Rust), and I need some help connecting the levels together.
For example, I have something like:
struct A {
x:...
2
votes
1
answer
96
views
Rust display trait performs poorly compared to `serde::serialize`
I've implemented a Rust clone of bgpdump that displays MRT messages in the same format as bgpdump. To print the message, I implemented the Display trait and printed it to stdout. However, I'm ...
1
vote
0
answers
48
views
Deserializing JSON object into single field [duplicate]
Is it possible to use Serde to deserialize following JSON into single simple struct instead of having nested structs?
JSON:
{
name: "Name",
server: {
name: "Server&...
2
votes
0
answers
114
views
How to deserialize a borrowed serde struct once and pass it across tokio::spawn tasks without cloning or DeserializeOwned?
I'm working with Rust and tokio to process incoming data from a channel (e.g., TCP). The data arrives as a String representing a JSON payload. My goal is to deserialize this JSON into a Rust struct ...
0
votes
0
answers
51
views
Serializing dates using rust's tauri-specta and apache arrow
I'm working on a Tauri application that uses tauri-specta for type safety and I can't figure out how to properly serialize dates. This is the file where most of the serialization and deserialization ...
3
votes
1
answer
148
views
How to create a trait that forces the implementation of serde serialize and deserialize to all of its implementations without crates
I want to create a trait that requires all of its implementations to derive serialize and deserialize form serde.
Additionally, the trait must also force the implementation of Debug and Clone.
This is ...
0
votes
0
answers
32
views
Dropped here while still borrowed for serde deserialize in Rust [duplicate]
I am trying to implement a function that requests a service by URL and deserializes the response into a structure. For a certain structure it works correct:
use serde::Deserialize;
#[derive(...
0
votes
1
answer
118
views
Force Rust serde to deserialize JSON Number to u16
How can I force serde to parse a JSON Number as a Rust u16 type?
Below I parse a JSON file. json_data is of type Value. This only has a as_u64() method, no as_u16(). As a result I first have to parse ...
0
votes
1
answer
62
views
Deserializing to T from json Object containing Vec<T> value
I have a json body object, which has a key called "symbols", and its value is Vec (T is also object). T contains a field called "symbol". I want to find the object containing a ...
1
vote
1
answer
85
views
How to get a list of all missing fields when deserializing a JSON with serde?
When handling JSON in Actix Web using serde, the default deserialization errors (missing field ...) do not provide a list of all missing fields. Instead, serde stops at the first missing field and ...
1
vote
2
answers
436
views
Gen enum string serialized value with serde
Currently working on a project where I need to serialize some types to a string from a dependency. I am trying to figure out the most efficient way to convert said types into a string while also ...
0
votes
0
answers
22
views
How do I use a DateTime wrapper in a struct field being serialized for use in Diesel? [duplicate]
I'm attempting to use a wrapper around chrono::DateTime<Utc> in a struct that I'm using as a model for a database row. I am using a wrapper so that I can implement other traits on it. However, I ...
0
votes
0
answers
65
views
Use enum variant as only its struct value [duplicate]
I have an app that has 2 functions that take a type we’ll call T and U. They are stored in an enum that looks like this:
#[derive(Serialize, Deserialize)]
#[serde(untagged)]
enum V {
T { w: String ...
0
votes
1
answer
158
views
How to deserialize a JSON input as a generic datastructure in Rust?
Most examples showing deserialization of JSON with Rust and serde_json show deserialization into a known structure format, using a defined struct.
Is it possible to deserialize a JSON document with an ...
1
vote
1
answer
147
views
serde through an error for unintended field
How to make serde through an error if the json string contains an extra field. This is my code
#[derive(Debug, serde::Deserialize)]
struct C {
a: A,
b: B,
}
#[derive(Debug, serde::Deserialize)...
1
vote
1
answer
77
views
How to deserialize a field that contains JSON?
Imagine I need to parse the following body:
{
"input": "{\"type\":\"id\",\"name\":\"DNI\"}"
}
As you can see, the input field contains a ...
3
votes
1
answer
88
views
Deserialize enum variant with and without fields
I have an Enum like the one below
#[serde(rename_all="snake_case")]
pub enum Flag {
One,
Two,
Three { value: Option<Decimal>}
}
I want to match the following json ...
2
votes
1
answer
248
views
Can serde_json::to_writer_pretty indent with tabs rather than 2 spaces?
Can serde_json::to_writer_pretty indent with tabs rather than 2 spaces?
let file = std::fs::File::create(&path).expect("Failed to create json file");
let mut w = BufWriter::new(...
0
votes
2
answers
188
views
How to use the `dyn` keyword for multiple traits in a vector when one trait has to be `Sized`?
My problem is: I have a function do_something, which expects a vector of objects. The objects can be of different types, but they all have to implement the traits A and B.
I declared examples of A and ...
0
votes
2
answers
129
views
Struct with dynamic fields
I have a code to convert json to struct using serde. But, got stuck at a json with dynamic keys.
The json will contain the price list. it can vary and will be dynamic
{"Price 1":{"price&...
1
vote
2
answers
565
views
How to deserialize a JSON encoded DateTime with a specified Timezone in Rust with serde?
Here is a JSON encoded object with a datetime and timezone. I believe this is an ISO 8601 format with 6 digits of precision for fractions of a second. (Microsecond precision.)
{
"time": &...
0
votes
1
answer
100
views
How do I deserialize JSON in Rust when combining a flattened component with the need to convert keys?
I apologize for somewhat unclear question, but I can't really phrase it well with one sentence. Below is a toy example illustrating the problem:
use serde_json;
use serde_json::Value;
use serde::{...
0
votes
1
answer
130
views
Associate unique identifier with rust function definitions
I have a struct that I would like to serialize to disk as part of a JSON save file format. The (simplified) structure of the struct is like this:
pub struct Effect {
pub source: Source,
pub ...
1
vote
1
answer
382
views
Serde serialize a HashMap like flatten but keeping the field name of the HashMap
Here is a Rust playground link with what I have so far.
I am working on a Rust project where the serialization/deserialization MUST be interoperable with a C# project. I have these two structs that ...