Questions tagged [serde]

Serde is a framework for serializing and deserializing Rust data structures efficiently and generically.

Serde is a framework for serializing and deserializing Rust data structures efficiently and generically.

Site

Repository

crates.io

839 questions
64
votes
2 answers

Generate pretty (indented) JSON with serde

Using the serde_json crate, I can use ::serde_json::to_string(&obj) to serialize an object into a JSON string. The resulting JSON uses compact formatting, like: {"foo":1,"bar":2} But how do I generate pretty/indented JSON? For example, I'd like to…
Jo Liss
  • 30,333
  • 19
  • 121
  • 170
47
votes
1 answer

How to transform fields during deserialization using Serde?

I'm using Serde to deserialize an XML file which has the hex value 0x400 as a string and I need to convert it to the value 1024 as a u32. Do I need to implement the Visitor trait so that I separate 0x and then decode 400 from base 16 to base 10? If…
phodina
  • 1,341
  • 1
  • 12
  • 25
47
votes
1 answer

How can I deserialize an optional field with custom functions using Serde?

I want to serialize and deserialize a chrono::NaiveDate with custom functions, but the Serde book does not cover this functionality and the code docs also do not help. #[macro_use] extern crate serde_derive; extern crate serde; extern crate…
VP.
  • 15,509
  • 17
  • 91
  • 161
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
39
votes
1 answer

How can I deserialize JSON with a top-level array using Serde?

I have a some JSON data that is returned from a web service. The JSON is a top-level array: [ { "data": "value1" }, { "data": "value2" }, { "data": "value3" } ] Using serde_derive to make structs I…
Noskcaj
  • 633
  • 1
  • 8
  • 10
38
votes
1 answer

How do I serialize or deserialize an Arc in Serde?

I have a struct that contains children of its own type. These children are wrapped in Arcs, and I'm getting issues when calling serde_json::to_string on it. My struct is: #[derive(Serialize, Deserialize)] pub struct Category { pub id: i32, …
dempzorz
  • 1,019
  • 13
  • 28
34
votes
1 answer

Why is a trait not implemented for a type that clearly has it implemented?

I'm trying to use Diesel to query a MySQL database and display the results with a Handlebars template with Rocket. I have this in models.rs #[derive(Queryable, Serialize)] pub struct Post { pub id: i32, pub title: String, pub text:…
haheute
  • 2,129
  • 3
  • 32
  • 49
30
votes
3 answers

How to deserialize a JSON file which contains null values using Serde?

I want to deserialize the chemical elements JSON file from Bowserinator on github using Serde. For this I created a structure with all the needed fields and derived the needed macros: #[derive(Serialize, Deserialize, Debug, Clone)] pub struct…
Hartmut
  • 473
  • 1
  • 6
  • 13
27
votes
1 answer

Is it possible to flatten sub-object fields while parsing with serde_json?

#[serde(rename)] seems to be the right option, but the documentation does not state if it is possible or how to do it. This JSON object: { "name" : "myobject" "info" : { "counter" : "3" "foo" : "bar" } } The corresponding…
eddy
  • 488
  • 5
  • 18
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
1 answer

Is there a way to deserialize arbitrary JSON using Serde without creating fine-grained objects?

I have a JSON object that contains a few metadata keys and a large data payload. My service cares about the metadata for the purposes of logging and routing, but does not care about the payload, other than to pass the payload off to another service.…
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
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
26
votes
2 answers

How to sort HashMap keys when serializing with serde?

I'm serializing a HashMap with serde, like so: #[derive(Serialize, Deserialize)] struct MyStruct { map: HashMap } HashMap's key order is unspecified, and since the hashing is randomized (see documentation), the keys actually end…
Jo Liss
  • 30,333
  • 19
  • 121
  • 170
26
votes
1 answer

How to transform fields during serialization using Serde?

How can I apply a transformation to a field before serialization? For example, how can I ensure that the fields lat and lon in this struct definition are rounded to at most 6 decimal places before being serialized? #[derive(Debug, Serialize)] struct…
Synesso
  • 37,610
  • 35
  • 136
  • 207
24
votes
4 answers

How can I distinguish between a deserialized field that is missing and one that is null?

I'd like to use Serde to parse some JSON as part of a HTTP PATCH request. Since PATCH requests don't pass the entire object, only the relevant data to update, I need the ability to tell between a value that was not passed, a value that was…
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
1
2 3
55 56