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
1 answer

How to rename all keys in a serde_json::Map?

Let's say I have a &mut std::collections::HashMap, and I want to turn all the keys into uppercase. The following code does the trick: use std::collections::HashMap; fn keys_to_upper(map: &mut HashMap) { let mut tmp =…
Anders
  • 8,307
  • 9
  • 56
  • 88
1
vote
0 answers

Rust | Crate: Serde Json | How to make custom parser for specific field?

so I stumble upon a problem where I need to instead of parsing pure value, run a function on that value and return the processed value from it into that field and I want to include that in the Serde JSON parser itself, I guess? confused emoji I'm…
user9419919
1
vote
1 answer

Parsing a JSON String with serde_json

pub fn rust_server() -> redis::RedisResult<()> { println!("-> redis_server"); let client = redis::Client::open(get_uri())?; let mut con = client.get_connection()?; println!("-> redis_server: {}", get_uri()); println!("->…
1
vote
1 answer

Serde Stream Deserializer for a type other than serde_json::Value

I want to parse a JSON string of the form { "a": { "foo": "bar" }, "b": { "foo": "baz" } } That is, at the top level there are a number of JSON objects separated by commas. Each of these objects have the same fields…
Charles German
  • 857
  • 10
  • 16
1
vote
2 answers

Functionally creating a nested object from a flat structure

I am attempting to turn a flat structure like the following: let flat = vec![ Foo { a: "abc1".to_owned(), b: "efg1".to_owned(), c: "yyyy".to_owned(), d: "aaaa".to_owned(), }, Foo { a:…
8176135
  • 3,755
  • 3
  • 19
  • 42
1
vote
1 answer

How to serialize std::env:vars() with serde_json?

How can I use serde_json to serialize std::env::vars() The following works but I feel like it could be done better: let mut vars = std::collections::HashMap::new(); for (key, value) in std::env::vars() { vars.insert(key, value); } json!(vars)
ryah
  • 11
  • 1
1
vote
1 answer

Skipping serialization of a variant if its value is empty? (custom serialization)

I want to skip serialization of Token variant if its value is empty. impl<'a> Serialize for UriParam<'a> { fn serialize(&self, serializer: S) -> Result where S: Serializer { match self { …
Ark
  • 117
  • 2
  • 7
0
votes
0 answers

serde_json serializer not working for structs defined in certain crates

In the process of testing these web assembly bindings in this tutorial application I discovered an issue with the serializers in this crate whenever I tested them in a JS environment. For context, here is how I discovered the issue: At first I…
0
votes
1 answer

Parallel json deserialization fails with valid json

I want to deserialize json values in parallel using rayon. A valid json from the serde-json example fails when trying to deserialize inside par_iter, despite being parsed correctly without parallelization. This is the code: use rayon::prelude::*; //…
ZJaume
  • 69
  • 5
0
votes
0 answers

What's the idiomatic way to derive try_from() and into()/try_into() for serde::json::Value?

I'm doing what I assume to be a very common thing and have value-type structs that will be serialized to JSON and deserialized from JSON. Is there a way to do that with #[derive()]? For a struct Foo: #[derive(Serialize, Deserialize/*,…
Jacob Phillips
  • 8,841
  • 3
  • 51
  • 66
0
votes
2 answers

API on Rust, error: "data limit exceeded"

I need to make an API on Rust, to which you give a string, it performs some function, and then returns a new string. But an error occurred, if the string is too long, then an error occurs: POST /test: >> Matched: (test) POST /test >> Data…
Duxtaa
  • 15
  • 3
0
votes
1 answer

Deserialize JSON list of `dyn T` structs?

I'm trying to learn Rust and have been building a raytracer as a beginner project to get my hands dirty with the language. I've built my raytracer following Peter Shirley's Ray Tracing in One Weekend The book is written in C++, I've tried my best to…
lmseper
  • 1
  • 1
0
votes
1 answer

Can't parse JSON from StackAPI: expected value", line: 1, column: 1

I'm trying to parse the JSON from this endpoint: https://api.stackexchange.com/2.2/users/13029516?&site=stackoverflow. It looks like this: { "items": [ { "badge_counts": { "bronze": 27, "silver": 14, "gold": 0 }, "account_id":…
Matthew Trent
  • 2,611
  • 1
  • 17
  • 30
0
votes
1 answer

Error: Error("EOF while parsing a value", line: 1, column: 0) while extracting a map from file with json_serde

To check how to use serde_json::to_writer() and serde_json::from_reader() I use a very simple main(), no problem when I just write but a strange error when I add the reader Error: Error("EOF while parsing a value", line: 1, column: 0) I just start…
jfboisvieux
  • 157
  • 1
  • 5
0
votes
1 answer

serde_json converting a String to a type with lifetime annotation causing issues

pub async fn checkin_seed_node<'a>(appstate: Arc>) { loop { let response: Response = call_seed_node(&appstate.client).await; let body = response .text() .await .expect("failed to…