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

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 { nums: Vec } fn main(){ let data = r#" …
Blank
  • 423
  • 1
  • 5
  • 16
3
votes
2 answers

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 and got it to work just fine for primitive types.…
Rick Rainey
  • 11,096
  • 4
  • 30
  • 48
3
votes
2 answers

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, "connections": [ { "color": "Any", "locomotives": 0, …
A. Gnias
  • 87
  • 3
  • 10
3
votes
1 answer

How do I get a SeqAccess type for an inner sequence of a serde SeqAccess?

I'm writing a library to parse json data that looks like this: {"x": [[1, "a"], [2, "b"]]} i.e. I have a key with a list of lists where the inner lists can contain different data types but each inner list has the same sequence of types. The…
wnorcbrown
  • 53
  • 4
3
votes
1 answer

Generic type that implements DeserializeOwned

Below is a non-functioning code example: use serde_json::json; #[derive(Serialize, Deserialize, Clone, Debug)] pub struct Model where T: DeserializeOwned, { pub id: i32, pub info: Option, } fn main() { #[derive(Serialize,…
Jakub Padło
  • 703
  • 3
  • 8
  • 16
3
votes
1 answer

serde_json flattening object with indices as keys

I have some json with from an external API that I would like to type. The shape of the data looks like: { "summary": { "field1": "foo", "field2": "bar", }, "0": { "fieldA": "123", "fieldB": "foobar" }, "1": { "fieldA":…
wileybaba
  • 380
  • 1
  • 3
  • 11
3
votes
1 answer

In rust deserialize flat JSON with optional parameter to a nested struct and enum

I'm trying to deserialize some JSON: {"id":1,"status":"Failed","cause":"Error"} where the status can be one of "Executing", "Successful" or "Failed" my issue is that I only get a "cause" if the status is "Failed". I don't want the cause to be an…
3
votes
1 answer

How to handle errors when extracting data from untyped JSON in serde_json?

I have a serde_json::Value which I expect to contain an array of objects. From those objects I want to extract 2 values and return an error if anything fails. This is my code so far: use std::collections::HashMap; use anyhow::Result; fn…
HelloImRandom
  • 97
  • 2
  • 6
3
votes
1 answer

Array of enum in a struct prints variant names and value

I have a struct that uses an enum, but when printing it gives the enum name and the value instead of just the value. I want to serialize it using serde_json to send as a JSON request. I want to re-use the struct for different commands to the geth…
DedicatedDreamer
  • 361
  • 5
  • 21
3
votes
1 answer

How to use serde_json with a union type like enum?

I have two structs that I want to serialize/deserialize with the tag as a "type" field in JSON, like so. #[derive(Debug, Clone, Serialize, Deserialize)] #[serde(tag = "type")] struct ThingA { value: usize, } #[derive(Debug, Clone, Serialize,…
danwoz
  • 75
  • 2
  • 6
3
votes
1 answer

How can I pass a json schema as data to actix web?

I want to pass a pre-compiled json schema to actix web, but the compiler complains that the borrowed Value used to create the JSONSchema does not live long enough. Is there a way to workaround this? Example: use jsonschema::JSONSchema; use…
Matt Roberts
  • 1,107
  • 10
  • 15
3
votes
2 answers

Implement Deserialize for a structure with Box

A struct containing Box as member variable: type Item = dyn Fn() -> Result<(), Box>; struct Inner { pub data: Box, } // a function, like the type: Item fn parse() -> Result<(), Box
bruceyuan
  • 421
  • 1
  • 3
  • 8
3
votes
0 answers

It is possible to discard items that fail to parse and continue parsing with serde_json?

I have a very large JSON file. Most of it is valid JSON data, but parts of it are not. The following is a simplification of my case: [ "this is valid: \ud835\udc47", "this is invalid: \ud835", ] The first item is valid and will be…
Johan Bjäreholt
  • 731
  • 1
  • 11
  • 24
3
votes
2 answers

Rust: Deserialize a JSON array into a very simple custom table

I'm trying to deserialize an array of arrays (represents a table of string cells) into a custom struct in Rust with serde_json. I know that using serde_json::Value is enough for this simple case but I would like to construct a custom type. use…
Natxo.Piq
  • 105
  • 2
  • 9
3
votes
1 answer

How do I serialize an enum without including the name of the enum variant?

I am trying to serialize an enum to a JSON string. I implemented Serialize trait for my enum as it is described in the docs, but I always get {"offset":{"Int":0}} instead of the desired {"offset":0}. extern crate serde; extern crate…
Jade
  • 283
  • 3
  • 9
  • 18
1 2
3
9 10