serde

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

北城余情 提交于 2019-12-13 16:46:23
问题 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": "1158266974317788", "messaging": [ { "sender": { "id": "some_id" }, "recipient": { "id": "some_id" }, "message": { "mid": "mid.$cAARHhbMo8SBllWARvlfZBrJc3wnP", "seq": 5728, "text": "test", "nlp": { "entities": {} // <-- here } } } ] } ] } This is my equivalent struct of the

How to use Serde to parse a YAML file with multiple different types? [duplicate]

非 Y 不嫁゛ 提交于 2019-12-13 15:17:25
问题 This question already has answers here : How can I use Serde with a JSON array with different objects for successes and errors? (2 answers) Parsing JSON with multiple representation in the same attribute (1 answer) How do I serialize an enum without including the name of the enum variant? (1 answer) Closed 9 months ago . I'm trying to parse this YAML file application: build: something container_name: another_thing environment: - ONE_ENV=fake - SEC_ENV=something I've created this code that

Can Serde deserialize JSON to one of a set of types depending on the value of a field?

与世无争的帅哥 提交于 2019-12-12 15:07:22
问题 I have a group of different messages that come in as JSON and can be distinguished based on a single field, but then each variant has a different collection of secondary fields: #[derive(Debug, Serialize, Deserialize)] struct MessageOne { ///op will always be "one" op: String, x: f64, y: f64, } #[derive(Debug, Serialize, Deserialize)] struct MessageTwo { ///op will always be "two" op: String, a: f64, b: i64, } The different message types are routed to different processing functions (e.g.

How do I deserialize a JSON array without needing a wrapper type? [duplicate]

别来无恙 提交于 2019-12-11 15:11:56
问题 This question already has an answer here : How can I deserialize JSON with a top-level array using Serde? (1 answer) Closed last year . I want to deserialize the following JSON: [ { "name": "one", "path": "/path/to/one" }, { "name": "two", "path": "/path/to/two" }, { "name": "three", "path": "/path/to/three" } ] Into a Vec<Worskpace> . Workspace is defined below: #[derive(Serialize, Deserialize)] struct Workspace { name: String, path: String, } Is there a way to do that without having to do

Lifetimes when Deserializing JSON within a FromForm

空扰寡人 提交于 2019-12-11 04:56:07
问题 I'm having trouble understanding the relationship between the lifetimes on this code. Basically, I have a Rocket API that receives some x-www-form-urlencoded data, with only one key: json . This key contains, intuitively, a JSON value, encoded with percent-encoding, of a struct Message<T> . (I'm aware this is suboptimal API design, but this is reverse-engineering work, so I have no option) To be easily used as a request guard as From<Message<T>> , I'm implementing FromForm . To do that, I

Rust and serde deserializing using generics

假如想象 提交于 2019-12-11 03:19:41
问题 I am trying to use generics to deserialize structs from file for use with a Swagger generated API. So I have hacked together this which almost works, but I am unable to unpack the external Struct object from the "Owned" pointer, as you can see in the tests. This might be the wrong strategy, but the problem is that I have various yaml files, which I want to read in and deserialise hinting the correct Struct to deserialise as. I don't want to implement a "readfile" function for each Struct, as

serde json deserialize any number [duplicate]

心不动则不痛 提交于 2019-12-11 02:07:09
问题 This question already has answers here : How to transform fields during deserialization using Serde? (1 answer) How do I serialize an enum without including the name of the enum variant? (1 answer) Why can Serde not derive Deserialize for a struct containing only a &Path? (1 answer) Closed 6 months ago . I am trying to combine the Either string or struct and the Manually deserialize struct examples by parsing something like: { "secs": "12.34" } Or { "secs": 5678 } Into the struct: struct

How can I mass implement Deserialize for all types that implement a specific trait?

折月煮酒 提交于 2019-12-10 22:24:25
问题 I am deserializing a YAML config file with Serde. For most structs I deserialize into, things are quite simple — there's a one-to-one relationship between the fields of the structs and the properties in my YAML file. In a few cases, things are a bit more complicated. For these, the properties in the YAML file are better viewed as parameters to the constructor. The actual struct will have different fields, calculated from those. For these cases, I have written separate config structs that I

How do I configure Serde to use an enum variant's discriminant rather than name?

馋奶兔 提交于 2019-12-10 19:08:49
问题 I'm parsing an INI-style file that uses integers for enumerators. #[derive(Debug, Deserialize, Serialize)] pub enum MyThing { First = 0, Second = 1, Third = 2, } In the file, the value will get serialized like so: thing=0 However, Serde by default matches against the variant name rather than the discriminant. Is custom-implementing Deserialize the cleanest method? 回答1: The Serde website has an entire example on how to serialize an enum as a number: extern crate serde; extern crate serde_json;

Deserialize TOML string to enum using config-rs

倖福魔咒の 提交于 2019-12-10 18:54:37
问题 I'm using config-rs to load the configuration from a TOML file and I want to deserialize a string to an enum. I tried to solve it using the deserialize_with feature of serde_derive but I don't know how to return a suitable Error to satisfy the function signature. How can I achieve it? My dependencies: config = "0.7" serde_derive = "1.0" serde = "1.0" toml = "0.4" Example code where it's intended to deserialize the enum RotationPolicyType : extern crate config; extern crate serde; #[macro_use]