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
2
votes
0 answers

Is there a way to specify field names to serde at runtime?

I'm trying to deserialise a JSON structure using serde, but where some of the field names can be different across different environments. For example: In dev: { "fields": { "field004": "This is the title", "field012": 456123 …
Sid Holland
  • 2,871
  • 3
  • 27
  • 43
2
votes
1 answer

How to deserialize a sequence into a custom type with Serde's SeqDeserializer?

I'm trying to write a custom serializer and derializer for a type which is uncomplicated but doesn't have a natural JSON representation. pub type ArrayKey = [i16; 5]; pub struct ArrayKeyedMap { the_map: HashMap } The HashMap,…
Joe
  • 46,419
  • 33
  • 155
  • 245
2
votes
0 answers

How do I deserialize a struct through its own `new` constructor?

I wish to deserialize the below struct, but using the new constructor to validate the deserialized fields: #[derive(Deserialize)] pub struct Timestamp { values: Vec, } impl Timestamp { pub fn new(values: Vec) -> Timestamp { …
ced73
  • 21
  • 1
2
votes
1 answer

"cannot find value __collect" when adding flatten to an enum struct variant

I have the following code: extern crate serde; #[macro_use] extern crate serde_derive; #[derive(Debug, Default, Serialize, Deserialize)] pub struct Base { bold: Option, } #[derive(Debug, Serialize, Deserialize)] #[serde(untagged)] pub…
wingerse
  • 3,670
  • 1
  • 29
  • 61
1
vote
1 answer

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 #[derive(Deserialize)] struct ApiResult { …
pielgrzym
  • 1,647
  • 3
  • 19
  • 28
1
vote
1 answer

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 static value but without success... See example…
Marco
  • 61
  • 7
1
vote
1 answer

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(json: &serde_json::value::Value, s: S) -> Result where S: Serializer, { let string =…
Michael
  • 13
  • 2
1
vote
1 answer

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: &str, payload: Vec) -> Value { let json =…
Jan
  • 93
  • 9
1
vote
1 answer

How to deserialize nested struct?

Assuming the following JSON should be read: let json = r#"{ "scjson": [ { "StateMachine": { "id": "sm_1" } }, { "StateMachine": { "id": "sm_2" } } ] }"#; In words: An array of StateMachine, with each StateMachine has a field…
user3054986
  • 417
  • 7
  • 18
1
vote
1 answer

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>, methods: Vec>, // other fields…
Rushmore75
  • 43
  • 4
1
vote
1 answer

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("expected value", line: 1, column: 1)…
1
vote
1 answer

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 then #[serde(rename_all = "camelCase")]. I can't…
Luis Nell
  • 534
  • 4
  • 12
1
vote
1 answer

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; #[derive(Deserialize)] pub struct…
user3690467
  • 3,049
  • 6
  • 27
  • 54
1
vote
1 answer

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: Option, pub payload: Box
code-gorilla
  • 2,231
  • 1
  • 6
  • 21
1
vote
1 answer

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 this struct: StructName { struct_name: 3 } Notice that…
Test
  • 962
  • 9
  • 26