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

convert an object to serde_json::Value without serializing and deserializing

I have this struct that has a field containing type Option I want to be able to store any object( i.e. object created by any struct) in that field. The current approach I'm using is to first convert the object to JSON string…
Mahesh Bansod
  • 1,493
  • 2
  • 24
  • 42
7
votes
3 answers

How can I stream elements from inside a JSON array using serde_json?

I have a 5GB JSON file which is an array of objects with fixed structure: [ { "first": "John", "last": "Doe", "email": "john.doe@yahoo.com" }, { "first": "Anne", "last": "Ortha", "email": "anne.ortha@hotmail.com" }, …
R Sun
  • 1,353
  • 14
  • 17
7
votes
1 answer

How to build json arrays or objects dynamically with serde_json?

I need to build a json object at runtime. For now, just a simple {"key": "stringvalue"} object. But each key/val pair must be added in a loop. This seems really simple/basic, but I didn't find any good examples or docs on it. I did finally…
danda
  • 485
  • 4
  • 13
6
votes
2 answers

How to iterate over JSON objects within a hierarchy?

I want to print the name of each contact in the object deep down the hierarchy. The contact object may not have the exact same number of fields every time to make a suitable structure. How can I achieve this? extern crate serde_json; use…
Rahul
  • 61
  • 1
  • 2
6
votes
1 answer

Parsing an object inside an object with serde_json

I am stuck, below is the JSON which I am receiving: { "BCH": { "aclass": "currency", "altname": "BCH", "decimals": 10, "display_decimals": 5 } } I am bit confused on how my struct should look like to parse the…
BalaB
  • 3,687
  • 9
  • 36
  • 58
5
votes
1 answer

How to partially deserialise a JSON object?

I have a JSON object: {"content":{"foo":1,"bar":2},"signature":"3f5ab1..."} Deserialising this into a custom type already works fine, using: let s: SignedContent = serde_json::from_str(string)?; What I want is {"foo":1,"bar":2} as a &[u8] slice,…
fadedbee
  • 42,671
  • 44
  • 178
  • 308
5
votes
1 answer

Is there a way of making serde_json deserialize strictly?

What I mean is that if 2 objects overlaps on some of the attributes is there a way to try to match all of them? For example: use serde::{Serialize, Deserialize}; use serde_json; // 1.0.47; // 1.0.104 #[derive(Serialize, Deserialize, Debug)] pub…
Netwave
  • 40,134
  • 6
  • 50
  • 93
5
votes
1 answer

borrowed value does not live long enough when use generic lifecycle。

borrowed value does not live long enough when use generic lifecycle。 fn get<'a, T>(&self, key: &[u8]) -> Result> where T: Deserialize<'a>, { match self.db.get(key)? { Some(dbv) => { let v =…
snlan
  • 151
  • 6
5
votes
1 answer

How do I change Serde's default implementation to return an empty object instead of null?

I'm developing an API wrapper and I'm having some troubles with the deserialization of an empty JSON object. The API returns this JSON object. Mind the empty object at entities: { "object": "page", "entry": [ { "id":…
kper
  • 313
  • 3
  • 12
4
votes
3 answers

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 format 100.36, i.e. Rupees 100 and paise 36. Some more…
snipedown21
  • 105
  • 8
4
votes
1 answer

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 tried rebuilding the project from scratch, cargo…
La-lo-go
  • 150
  • 11
4
votes
2 answers

Can you deserialize a struct from a map or a string?

Consider this Config struct which contains a vector of Host structs: use serde::Deserialize; use std::net::IpAddr; #[derive(Debug, Deserialize)] struct Config { name: String, hosts: Vec } #[derive(Debug, Deserialize)] struct Host { …
4
votes
1 answer

Is it possible to deserialize part of a JSON object with serde_json?

Assume following JSON: { "person": { "first_name": "Ala", "last_name": "Makota" } } Is it possible to deserialize this object to a struct like following, skipping "person"? #[derive(Deserialize)] struct Person { first_name: String, …
smbear
  • 1,007
  • 9
  • 17
4
votes
1 answer

Using serde_json::from_str to deserialize into a struct with a &'static string has a lifetime error

I am attempting to test a JSON bridge which I set up for use with Dart, JS and Python UI frameworks. It works fine for those, but when I attempted the same UI/logic split in a Rust program using tui-rs, I get a lifetime error when trying to…
3
votes
1 answer

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 null. And serde_json::json! panics with message "key…
yumaa
  • 965
  • 9
  • 18
1
2
3
9 10