Questions tagged [serde-json]
A Rust library for parsing and generating the JSON (JavaScript Object Notation) file format.
139
questions
-1
votes
0
answers
29
views
how to fix error moved value when converting response to json? [duplicate]
here i`m getting error, my plan was to check if json structure throw error if not, just display text of response in future i will convert this text to structure like error too, but for now i just want ...
1
vote
1
answer
75
views
Deserialize into a generic type with serde::from_value
I want to deserialize API json response into a struct that uses generic type. Code below does not compile and I can't figure out how to make it work:
use serde::Deserialize;
use serde_json; // 1.0.102
...
0
votes
0
answers
18
views
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, ...
2
votes
1
answer
64
views
Deserialization of optionally-wrapped enum
I have a Rust web service endpoint which accepts a JSON payload. The payload contains a nested enum structure, something like:
#[derive(Serialize, Deserialize, Debug, PartialEq)]
#[serde(...
1
vote
1
answer
41
views
Skip serializing at runtime with `skip_serializing_if`?
I read about serde::skip_serializing_if and I would like to iplement it into my project, however I did not find a way to read the value at runtime (imagine a flag --ignore-practices).
I tried with a ...
0
votes
1
answer
42
views
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 ...
0
votes
2
answers
84
views
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:
...
1
vote
1
answer
87
views
How do you propagate errors in a Rust serde serializer?
I'm trying to implement serde's serialize_with attribute.
I have this code:
use serde::Serializer;
pub fn serialize_json_as_string<S>(json: &serde_json::value::Value, s: S) -> Result<...
0
votes
1
answer
71
views
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 ...
0
votes
1
answer
166
views
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": {
&...
0
votes
1
answer
369
views
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 ...
2
votes
1
answer
359
views
How can I build up a stateful, streaming parser with serde_json?
I'm trying to do some stateful JSON parsing with serde and serde_json. I started by checking out How to pass options to Rust's serde that can be accessed in Deserialize::deserialize()?, and while I ...
0
votes
1
answer
70
views
serde_json converting a String to a type with lifetime annotation causing issues
pub async fn checkin_seed_node<'a>(appstate: Arc<AppState<'a>>) {
loop {
let response: Response = call_seed_node(&appstate.client).await;
let body = response
...
3
votes
1
answer
210
views
serde_yaml::Value -> serde_json::Value, `null` keys
I have big complex YAML file, which I successfully read into serde_yaml::Value and need to send to the user (from Axum handler) as JSON.
But somewhere deep inside that big YAML there are keys like ...
-2
votes
1
answer
333
views
Rust Error("missing field `key`") when deserializing to JSON
I already tried this solution but it didn't work.
I always get the error Error("missing field key") and want to know how to fix this.
Code:
pub fn get_user(id: String) -> Data {
let ...
1
vote
1
answer
975
views
Avoid/remove escape quotes in serde_json
I try to create a JSON object with serde_json::json! but the problem is that I get \" but I don't want them. How can I prevent or remove them?
fn create_cache_json(token: &str, change: &...
0
votes
1
answer
361
views
Problems working with rust and postgres data types
I'm triying to do an Api REST with rust and postres but I cant make it work because the relation between these two.
The actual problem is that I have a column in postgres as jsonb and when I return ...
0
votes
2
answers
155
views
How to construct a serde struct with this type of JSON format?
I'm trying to construct a JSON in rust using serde using structs.
{
"parent": { "database_id": "123" },
"properties": {
"title": {
&...
0
votes
1
answer
142
views
Why does serde::json need to copy this ref? [duplicate]
Here's some simple code that seems like it should work:
use serde_json;
use std::io::Write;
fn test(writer: &mut dyn Write) {
serde_json::to_writer(writer, "test1").unwrap();
...
3
votes
1
answer
174
views
serde how to error out when deserializing empty arrays
I want de-serialization to fail empty arrays.
The following code works, but I want to make this specific case fail:
use serde::Deserialize;
#[derive(Debug)]
#[derive(Deserialize)]
struct Doc {
...
1
vote
1
answer
455
views
How to deserialize nested struct?
Assuming the following JSON should be read:
let json = r#"{
"scjson": [
{ "StateMachine": { "id": "sm_1" } },
{ "StateMachine"...
0
votes
1
answer
172
views
Panic while deserializing JSON data
I'm currently trying to use some API keys to access the Kucoin exchange from my program.I want to be able to have a global initialization of the keys so I can reuse them as credentials in different ...
0
votes
1
answer
215
views
How to parse a sequence of anonymous enum values into a tuple struct? [duplicate]
Is it possible to parse a JSON array into a tuple struct with a vector of enums?
use serde::Deserialize;
#[derive(Deserialize, Debug, PartialEq, Eq)]
enum Data {
Single(i32),
List(Vec<Data&...
1
vote
1
answer
209
views
How can I serialize multiple vectors into a single sequence with serde?
Trying to serialize two different Vec fields into a single array in the JSON output. I can't figure out how to implement the serialize() method:
struct Base<'a> {
workspace: Vec<Workspace&...
-1
votes
1
answer
304
views
Access JSON array in serde_json::Value
I have some JSON formatted like this:
[
{
"name": "jsonvalue"
}
]
And I want to print it like println!("{:?}", json["name"]);. How do I access ...
1
vote
1
answer
1k
views
Parse json in rust with reqwest and serde_json
I am trying to retrieve and parse a JSON file using reqwest.
I used this question as a starting point but it doesn't work with my API.
The error:
Error: reqwest::Error { kind: Decode, source: Error(&...
1
vote
1
answer
359
views
Convert serde_json Value keys to camelCase
I'm writing a CLI tool that reads JSON files and is supposed to convert the JSON object keys into camelCase.
Because this should work with any JSON file, I obviously can't just use strong typing and ...
4
votes
3
answers
813
views
How to serialize a struct containing f32 using serde_json?
Relatively new to Rust. I am trying to make an API call which requires the JSON body to be serialized.
The JSON body contains an order_amount key with value which can only take values having INR ...
1
vote
1
answer
321
views
Parse missing value in JSON as unit-struct
Unit structs don't have a value, however serde is unable to parse them from missing values. I have to explicitly set the value to null. For example:
use serde::Deserialize;
use serde_json::json;
#[...
0
votes
1
answer
513
views
Serde Json visitor to derserialise into generic struct
I have a nested JSON structure, the relevant bits of which look like this:
{
"params": {
"foo": {
"type": "integer"
},
"bar": {
...
4
votes
1
answer
2k
views
unresolved import `serde`/`serde_json`
I build the project without any visible errors but when compiling it I encounter the error E0432 that tells me that serde and serde_json are not found when they have been declared in the Cargo.toml. I ...
3
votes
2
answers
470
views
Serialize a remote struct with private String
I need to serialize a struct from a remote crate and all of the fields in the struct are private. There are getter's implemented in the remote struct to get those values. I am following this guidance ...
2
votes
1
answer
591
views
Using IndexMap with serde_json
I would like to use serde_json to parse a .json file into a IndexMap. However it says the Deserialize trait has not been implemented for IndexMap. Is there a way to use serde_json with IndexMap?
let ...
0
votes
1
answer
296
views
Use simd-json with reqwest
How can one get the Rust reqwest library to parse json via simd-json? I believe the current default is serde_json.
reqwest::get("https://www.example.com").await.json().await
If it is ...
1
vote
1
answer
437
views
Rust/Serde: Serialize events with polymorphic payload
I have events with a polymorphic payload property:
use serde::{Deserialize, Serialize};
#[derive(Debug, Deserialize, Serialize)]
pub struct Event<'a> {
pub topic: String,
pub key: ...
1
vote
1
answer
1k
views
Can you map serde_json names to different struct values?
In the serde_json library, is it possible to parse json and map one property name to a different property name in the Rust struct?
For example, parse this json:
{
"json_name": 3
}
into ...
1
vote
2
answers
125
views
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 ...
0
votes
1
answer
247
views
Remove duplicates from array of serde_json::Value in Rust
I have an array of generic serde_json::Value, that may also contain duplicates.
serde_json::Value does not implement std::cmp::Ord and I also cannot implement the trait because only traits defined in ...
0
votes
1
answer
520
views
Deserialize f64 using serde and divide by 100
Id like to divide my_val by 100 on deserialization:
#[derive(Debug, Deserialize, Clone)]
pub struct MyObject {
pub other: String,
pub my_val: f64, // <-- divide by 100.0
}
I found ...
2
votes
2
answers
765
views
How to rename `start` and `end` range values with serde?
I have JSON objects with the following format:
{
"name": "foo",
"value": 1234,
"upper_bound": 5000,
"lower_bound": 1000
}
I'd like to use ...
1
vote
1
answer
182
views
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::<Vec<...
3
votes
2
answers
6k
views
Parsing a nested JSON object
I'm trying to parse a JSON file with the following loose format using serde_json in Rust:
{
"Source_n": {
"Destination_n": {
"distance": 2,
"...
2
votes
2
answers
2k
views
rust: adding a field to an existing struct with serde_json
I have a pre-defined struct
use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize)]
struct Foo<T>
where T: Serialize
{
foo: T
}
struct Bar{
a: String
}
struct ...
1
vote
1
answer
808
views
Deserializing a JSON field with multiple elements from Strings to a Vec of Vec<u8>s
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 ...
1
vote
0
answers
560
views
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 ...
1
vote
1
answer
2k
views
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 ...
1
vote
2
answers
529
views
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 ...
1
vote
0
answers
60
views
Deserialize a field that sometimes has quotations around the value [duplicate]
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 {
...
0
votes
1
answer
966
views
Getting "trailing characters" error when trying to parse string with serde_json
I need to convert a specific string in serde_json::Value, but getting a trailing character error while using:
let new_str = "73723235c81ebbec0"
let json_value: serde_json::Value = serde_json:...
0
votes
1
answer
463
views
How to implement Iterator in Rust for third party library [duplicate]
Im trying to make the following work
use serde_json::Value;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let json = reqwest::get("https://www....