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
39
votes
4 answers

How do I use Serde to serialize a HashMap with structs as keys to JSON?

I want to serialize a HashMap with structs as keys: use serde::{Deserialize, Serialize}; // 1.0.68 use std::collections::HashMap; fn main() { #[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Hash)] struct Foo { x: u64, } …
YjyJeff
  • 833
  • 1
  • 6
  • 14
27
votes
3 answers

How do I avoid generating JSON when serializing a value that is null or a default value?

The serde_json::to_string() function will generate a string which may include null for an Option, or 0 for a u32. This makes the output larger, so I want to ignore these sorts of values. I want to simplify the JSON string output of the following…
llxxbb
  • 1,241
  • 1
  • 10
  • 16
26
votes
3 answers

How can I deserialize an enum when the case doesn't match?

I have a JSON structure that looks like this: { "type": "suite", "event": "started", "test_count": 1 } I want to deserialize into these structs: #[derive(Debug, Deserialize)] enum ResultType { Suite, Test, } #[derive(Debug,…
CaseyB
  • 24,780
  • 14
  • 77
  • 112
26
votes
2 answers

How to fix lifetime error when function returns a serde Deserialize type?

I'm using serde and serde_json 1.0 to decode data from a base64 string: fn from_base64_str(string: &str) -> T { let slice = decode_config(string, URL_SAFE).unwrap(); serde_json::from_slice(&slice).unwrap() } When I…
realli
  • 990
  • 6
  • 18
23
votes
1 answer

"invalid type: map, expected a sequence" when deserializing a nested JSON structure with Serde

I am trying to poll the GitHub API for issues and print them out. To do so, I need to deserialize a nested JSON structure that I receive from a cURL GET request. I am trying to get the url for all the objects in the items array: { "total_count":…
zep
  • 271
  • 1
  • 3
  • 8
16
votes
1 answer

Rust handling error response bodies with Reqwest

I'm using the reqwest (version 0.10.4) crate for the HTTP calls in my Rust application but can't find any examples of how to handle APIs calls that could return more than one possible response body, mainly for error handling. For instance, an API…
m_callens
  • 6,100
  • 8
  • 32
  • 54
14
votes
1 answer

How can I accept multiple deserialization names for the same Serde field?

I am trying to use Serde to deserialize JSON (serde-json) and XML (serde-xml-rs) files based on the following struct: use serde_derive::Deserialize; #[derive(Debug, Clone, PartialEq, Deserialize)] pub struct SchemaConfig { pub name: String, …
MarcioPorto
  • 555
  • 7
  • 22
14
votes
4 answers

Deserialize a JSON string or array of strings into a Vec

I'm writing a crate that interfaces with a JSON web API. One endpoint usually returns responses of the form { "key": ["value1", "value2"] }, but sometimes there's only one value for the key, and the endpoint returns { "key": "value" } instead of {…
Arnavion
  • 3,627
  • 1
  • 24
  • 31
13
votes
2 answers

Is there is a simpler way to convert a type upon deserialization?

Using serde_json, I have JSON objects with Strings that I need to convert to floats. I've stumbled upon a custom deserializer solution, but it seems like a hack. Here is a working playground example of the code below. #[macro_use] extern crate…
user1992266
  • 243
  • 2
  • 9
12
votes
1 answer

Parsing a JSON into a Map in rust

I'm quite a beginner in Rust, and just have encountered a problem with parsing JSON files. I tried using serde_json for the task. I know how to parse an ASCII file as a string, and how to parse its content as a Value, but I need a Map
Gabor Farkas
  • 225
  • 4
  • 9
11
votes
3 answers

How can I merge two JSON objects with Rust?

I have two JSON files: JSON 1 { "title": "This is a title", "person" : { "firstName" : "John", "lastName" : "Doe" }, "cities":[ "london", "paris" ] } JSON 2 { "title": "This is another title", "person" : { "firstName" :…
Harindaka
  • 4,658
  • 8
  • 43
  • 62
10
votes
2 answers

Get name of enum variant as string with serde

I'm trying to get the name of an enum variant as the string serde would expect/create. For example, say I have the following enum: #[derive(Serialize, Deserialize)] #[serde(rename_all="camelCase")] pub enum SomeEnum { WithoutValue, …
Mendy
  • 7,612
  • 5
  • 28
  • 42
10
votes
2 answers

How can I deserialize a type where all the fields are default values as a None instead?

I have to deserialize JSON blobs where in some places the absence of an entire object is encoded as an object with the same structure but all of its fields set to default values (empty strings and zeroes). extern crate serde_json; //…
main--
  • 3,873
  • 1
  • 16
  • 37
9
votes
1 answer

How do you make a Serde struct work with both borrowed and owned data?

I want to make a Serde struct that is capable of being deserialized from either borrowed data (serde_json::from_str) or owned data (serde_json::from_reader). I have read Understanding deserializer lifetimes, and I understand the difference between…
sffc
  • 6,186
  • 3
  • 44
  • 68
9
votes
3 answers

What's the difference between using the return statement and omitting the semicolon in Rust?

I'm writing a function that returns a serde_json::Value upon success (and failure). Previously in Rust I have been omitting the semicolon to return data from a function, like in the code example below: use serde_json::{Result, Value}; use…
Newbyte
  • 2,421
  • 5
  • 22
  • 45
1
2 3
9 10