Questions tagged [serde-json]

A Rust library for parsing and generating the JSON (JavaScript Object Notation) file format.

A Rust library for parsing and generating the JSON (JavaScript Object Notation) file format. It is built upon Serde, a high performance generic serialization framework.

https://github.com/serde-rs/json

139 questions
1
vote
2 answers

serde_json adding an extra '}'

I have a problme serializing/desserializing an struct with serde_json in rust, i had used the impl's (dont used derive) that are on serde_json:ser and serde_json:de, but something is happening when i try to write it in a file, i hava thread (this is…
Dykeiichi
  • 21
  • 1
1
vote
1 answer

Reading an array of strings from a file in Rust

I have the following JSON: [ "String1", "String2", "String3" ] I'm trying to read it in Rust with serde_json like this: serde_json::from_str::>("file_path").unwrap(); However, I get the following error: thread 'main'…
A. Gnias
  • 87
  • 3
  • 10
1
vote
1 answer

Deserializing a JSON field with multiple elements from Strings to a Vec of Vecs

I have a json structure that follows the following example: { "title": "This is the title of the document", "content": "This is a much longer entry containing the full content of a document", "version_author":…
jpataylor
  • 113
  • 3
1
vote
0 answers

How can I deserialize multiple variants of a struct based on the value of a tag inside the struct itself using serde/quick-xml?

I am using quick_xml to deserialize some xml into their respective structs. I have implemented a enum variant and have implemented Deserialize myself for the enum. I would like to deserialize the enum based on the value in one of the tags inside the…
H.Z.
  • 81
  • 4
1
vote
1 answer

Serialize/Deserialize json array to struct

I have incoming data in a json array that I deserialize into a struct, but I can't seem figure out how to serialize it back into an array instead of an object. Do I have to implement a custom serializer here or is there some other serde attribute I…
1
vote
2 answers

Deserialize using a function of the tag

An API with this internally tagged field structure, with "topic" being the tag: { "topic": "Car" "name": "BMW" "HP": 250 } This can be deserialized with #[derive(Serialize, Deserialize)] #[serde(tag = "topic")] pub enum catalog { …
Carlos
  • 5,991
  • 6
  • 43
  • 82
1
vote
0 answers

Deserialize a field that sometimes has quotations around the value

I'm consuming a JSON API that looks like this: { "timestamp": 1650057633185497 } This will work fine with a struct that looks like: #[derive(Clone, Debug, Deserialize)] pub struct Msg { pub timestamp: i64 } Unfortunately the server…
Carlos
  • 5,991
  • 6
  • 43
  • 82
1
vote
1 answer

How to express variable type in match arm?

I'm trying to parse a piece of json string using serde_json in Rust. I want to match the result of the parse using the following syntax: match serde_json::from_str(msg.to_text().unwrap()) { Ok(result) => { println!("Parsed: {}",…
s4eed
  • 7,173
  • 9
  • 67
  • 104
1
vote
1 answer

Capture original payload through serde

I wonder whether there's a way to preserve the original String using serde_json? Consider this example: #[derive(Debug, Serialize, Deserialize)] struct User { #[serde(skip)] pub raw: String, pub id: u64, pub login: String, } { …
Evaldas Buinauskas
  • 13,739
  • 11
  • 55
  • 107
1
vote
0 answers

How can we control the display of structs serialized with serde_json?

Some brief context: we're storing uuids in our database; these function as ids. We'd like to present these to various http clients as base62 encoded representations. Ideally we wouldn't have to remember to do this every time we encode a struct. It's…
maxcountryman
  • 1,562
  • 1
  • 24
  • 51
1
vote
1 answer

How to deserialize JSON into a structure with a Box<[u8]> value?

I have a struct that needs to include a "bytes" field, and I'm trying to deserialize it from JSON. When I use &'a [u8], this works, but then I need to add a lifetime annotation to this struct, and the struct that encloses it, and so on. I thought…
agam
  • 5,064
  • 6
  • 31
  • 37
1
vote
0 answers

How to lazily deserialize from a JSON array?

Problem description Using serde_json to deserialize a very long array of objects into a Vec can take a long time, because the entire array must be read into memory up front. I'd like to iterate over the items in the array instead to avoid the…
1
vote
1 answer

Inserting serde_json object in array in Rust

So I have a problem. I have a base serde_json Value that goes like this: { "person": [ { "country": "Jamaica", "name: "John", }, ... ], ... } I can easily insert one object as: { "person": …
Kramen
  • 33
  • 4
1
vote
2 answers

How can I dynamically define the struct for serde_json when the JSON structure is changed without recompiling?

For example, we add a structure that is used inside a *.rs script #[derive(Serialize, Deserialize, Debug)] struct Foo { Output: OUTPUT, input: INPUT, logs: LOGS, } #[derive(Serialize, Deserialize, Debug)] struct OUTPUT { width:…
art vanderlay
  • 2,341
  • 4
  • 35
  • 64
1
vote
1 answer

serde_json to json prints addtitonal String \r and \n in diesel db object

Playground link use serde_json::json; // 1.0.57 fn main() { let users = vec![Users { id : 10, username : "test".to_string(), password : "pass".to_string() }]; for user in &users { println!("I print id:{},password:{},username:{}…
Stuhl
  • 39
  • 5