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

How do I customize a Deserialize implementation for certain input types but not all of them?

I have a type like this, although my actual type is bigger and more complex: struct MyType { i: u32, } If I implement Deserialize for this type, serde looks for something like this (I'm interested in JSON): {"i":100} I want to customize it…
wingerse
  • 3,670
  • 1
  • 29
  • 61
3
votes
1 answer

Deserialize JSON array into tuple and give it type tags

I have JSON that looks like: [{"range": [1, 2]}, {"range": [2, 5]}] The objects in array have fields other than range of course, but it doesn't matter. Would it be possible to deserialize them into tuples that have two phantom types to indicate…
user1685095
  • 5,787
  • 9
  • 51
  • 100
3
votes
1 answer

Function local variable doesn't live long enough

I'm trying to write a wrapper around serde_json & Rocket's FromData to strongly type some of the JSON I exchange with a server. I can't compile the following code: extern crate serde_json; extern crate rocket; extern crate serde; use…
Boris
  • 991
  • 1
  • 9
  • 15
2
votes
1 answer

Deserialization of optionally-wrapped enum

I have a Rust web service endpoint which accepts a JSON payload. The payload contains a nested enum structure, something like: #[derive(Serialize, Deserialize, Debug, PartialEq)] #[serde(rename_all = "snake_case")] enum InnerEnum { …
superstator
  • 3,005
  • 1
  • 33
  • 43
2
votes
1 answer

How can I build up a stateful, streaming parser with serde_json?

I'm trying to do some stateful JSON parsing with serde and serde_json. I started by checking out How to pass options to Rust's serde that can be accessed in Deserialize::deserialize()?, and while I nearly have what I need, I seem to be missing…
user655321
  • 1,572
  • 2
  • 16
  • 33
2
votes
1 answer

Using IndexMap with serde_json

I would like to use serde_json to parse a .json file into a IndexMap. However it says the Deserialize trait has not been implemented for IndexMap. Is there a way to use serde_json with IndexMap? let elements: IndexMap =…
2
votes
2 answers

How to rename `start` and `end` range values with serde?

I have JSON objects with the following format: { "name": "foo", "value": 1234, "upper_bound": 5000, "lower_bound": 1000 } I'd like to use serde to work with these objects, with a struct like struct MyObject { name: String, value: i32, …
breadjesus
  • 1,979
  • 3
  • 13
  • 8
2
votes
2 answers

rust: adding a field to an existing struct with serde_json

I have a pre-defined struct use serde::{Deserialize, Serialize}; #[derive(Debug, Serialize, Deserialize)] struct Foo where T: Serialize { foo: T } struct Bar{ a: String } struct Bar2{ b: String } fn main() -> Result<()> { let…
dexhunter
  • 578
  • 8
  • 24
2
votes
1 answer

Cannot write file without appending to it

I am trying to write to a json file but it kepts appending to the files instead of overwriting it, even when I set append to false What did i do wrong? thx use serde::{Deserialize, Serialize}; use serde_json::{json, Map, Value}; use sha2::{Digest,…
Siriusmart
  • 187
  • 1
  • 12
2
votes
2 answers

Deserialising JSON in a different format - Serde_JSON

I am trying to read JSON from a file in Rust which has the following dimensions: { "DIPLOBLASTIC":"Characterizing the ovum when it has two primary germinallayers.", "DEFIGURE":"To delineate. [Obs.]These two stones as they are here defigured.…
2
votes
0 answers

How can I use serde_json to deserialize arbitrary JSON into a Value-like object of raw bytes?

I'm writing a library to deserialize a subset of JSON into predefined Python types. I want to deserialize arbitrary JSON into an object that quacks like serde-json's Value. However, I don't want it to deserialize into String's, Number's and Bool's -…
wnorcbrown
  • 53
  • 4
2
votes
1 answer

Exporting HashMap of HashMap to Python

I have a text parser written in Rust and want to provide a Python interface to it using pyo3. The parser returns a HashMap within a HashMap and the values of the inner HashMap are of type serde_json::Value. When I try to return this as a PyObject I…
leviathan
  • 363
  • 3
  • 10
2
votes
1 answer

How can I parse a JSON array of either strings or objects?

An API I hit has poorly structured JSON. Someone decided that it was a great idea to send back a list that looks like this features: [ "First one", "second one", { "feature": "third one", "hasAdditionalImpact": true }, "forth…
ngin
  • 33
  • 4
2
votes
1 answer

Make serde only produce hex strings for human-readable serialiser?

I'm currently using serde-hex. use serde_hex::{SerHex,StrictPfx,CompactPfx}; #[derive(Debug,PartialEq,Eq,Serialize,Deserialize)] struct Foo { #[serde(with = "SerHex::")] bar: [u8;4], #[serde(with = "SerHex::")] …
fadedbee
  • 42,671
  • 44
  • 178
  • 308
2
votes
2 answers

Is it possible to override default trait implementations in Rust?

I haven't wandered the path of advanced traits much, but I'm wondering if it's possible to save re-writing / copying and pasting nine functions by creating a trait which overrides just one or three functions of a more complex trait. This was some…
RandomInsano
  • 1,204
  • 2
  • 16
  • 36