serde

Rust serde deserializing a mixed array

北慕城南 提交于 2021-02-16 15:10:26
问题 In Rust i am receiving data from a websocket. For simplicity it looks like this: [1, {"a": ["1.2345", 5, "9.8765"]}] The string i get from the websocket is indeed double-quoted 'floating point values' (thus in actuality strings), and unquoted integers. I want to deserialize this object into a struct. But since the return array "a" is of mixed type, I can't use something like: struct MyStruct { id: i32, a: [f64; 3], } So I thought let's define another struct: struct Ask { price: f64, whole_lot

“invalid type: map, expected a sequence” when deserializing a nested JSON structure with Serde

点点圈 提交于 2021-02-16 10:49:30
问题 I am trying to poll the GitHub API for issues and print them out. To do so, I need to deserialize a nested JSON structure that I receive from a cURL GET request. I am trying to get the url for all the objects in the items array: { "total_count": 4905, "incomplete_results": false, "items": [ { "url": "https://api.github.com/repos/servo/saltfs/issues/789", "repository_url": "https://api.github.com/repos/servo/saltfs", "labels_url": "https://api.github.com/repos/servo/saltfs/issues/789/labels{

“invalid type: map, expected a sequence” when deserializing a nested JSON structure with Serde

左心房为你撑大大i 提交于 2021-02-16 10:48:10
问题 I am trying to poll the GitHub API for issues and print them out. To do so, I need to deserialize a nested JSON structure that I receive from a cURL GET request. I am trying to get the url for all the objects in the items array: { "total_count": 4905, "incomplete_results": false, "items": [ { "url": "https://api.github.com/repos/servo/saltfs/issues/789", "repository_url": "https://api.github.com/repos/servo/saltfs", "labels_url": "https://api.github.com/repos/servo/saltfs/issues/789/labels{

How can I use serde to deserialize into a hierarchical decentralized configuration? [duplicate]

谁说我不能喝 提交于 2021-02-10 23:26:52
问题 This question already has answers here : How can deserialization of polymorphic trait objects be added in Rust if at all? (3 answers) How do I deserialize into trait, not a concrete type? (3 answers) Closed 6 months ago . I want to have a JSON config file of the form: { "general_option_foo": true, "general_option_bar": "hello world!", "modules": [ { "name": "moduleA", "module_options" : {"optFoo":"foo"} }, { "name" : "moduleB", "module_options" : {"optBar":[3,4], "optBuzz": true} } { "name":

how to parse a YAML containing a simple list together with a key-value list (associative array)

淺唱寂寞╮ 提交于 2021-02-05 11:52:47
问题 I have the following YAML: build: - step 1 - step 2 - name: step 3 do: something - name: step 4 get: fetch ... - name: step 5 put: upload something And for parsing it I am trying with this #[derive(Debug, Deserialize, PartialEq)] struct Config { build: Option<Vec<Steps>> } #[derive(Debug, Deserialize, PartialEq)] struct Build { #[serde(flatten)] item: String, steps: Steps, } #[derive(Debug, Deserialize, PartialEq)] struct Steps { name: String, r#do: Option<String>, put: Option<String>, get:

how to parse a YAML containing a simple list together with a key-value list (associative array)

落爺英雄遲暮 提交于 2021-02-05 11:52:03
问题 I have the following YAML: build: - step 1 - step 2 - name: step 3 do: something - name: step 4 get: fetch ... - name: step 5 put: upload something And for parsing it I am trying with this #[derive(Debug, Deserialize, PartialEq)] struct Config { build: Option<Vec<Steps>> } #[derive(Debug, Deserialize, PartialEq)] struct Build { #[serde(flatten)] item: String, steps: Steps, } #[derive(Debug, Deserialize, PartialEq)] struct Steps { name: String, r#do: Option<String>, put: Option<String>, get:

How to conditionally deserialize JSON to two different variants of an enum?

邮差的信 提交于 2021-02-04 08:10:15
问题 Let's say I have JSON data like the following: { "type": "A", "value": [ 1, 2, 3, 4, 5 ] } { "type": "B", "value": [ [ 1, 2, 3, 4, 5 ], [ 6, 7, 8 ] ] } type determines the type of value , which in the first example is Vec<u32> and in the second is Vec<Vec<u32>> . If I represent the above data as follows: enum DataValue { TypeA(Vec<u32>), TypeB(Vec<Vec<u32>>) } struct Data { data_type: String, value: DataValue } How do I implement serde deserialization to properly decode these values? 回答1: You

How to conditionally deserialize JSON to two different variants of an enum?

ぃ、小莉子 提交于 2021-02-04 08:05:04
问题 Let's say I have JSON data like the following: { "type": "A", "value": [ 1, 2, 3, 4, 5 ] } { "type": "B", "value": [ [ 1, 2, 3, 4, 5 ], [ 6, 7, 8 ] ] } type determines the type of value , which in the first example is Vec<u32> and in the second is Vec<Vec<u32>> . If I represent the above data as follows: enum DataValue { TypeA(Vec<u32>), TypeB(Vec<Vec<u32>>) } struct Data { data_type: String, value: DataValue } How do I implement serde deserialization to properly decode these values? 回答1: You

How to conditionally deserialize JSON to two different variants of an enum?

孤人 提交于 2021-02-04 08:04:26
问题 Let's say I have JSON data like the following: { "type": "A", "value": [ 1, 2, 3, 4, 5 ] } { "type": "B", "value": [ [ 1, 2, 3, 4, 5 ], [ 6, 7, 8 ] ] } type determines the type of value , which in the first example is Vec<u32> and in the second is Vec<Vec<u32>> . If I represent the above data as follows: enum DataValue { TypeA(Vec<u32>), TypeB(Vec<Vec<u32>>) } struct Data { data_type: String, value: DataValue } How do I implement serde deserialization to properly decode these values? 回答1: You

How to conditionally deserialize JSON to two different variants of an enum?

痞子三分冷 提交于 2021-02-04 08:04:24
问题 Let's say I have JSON data like the following: { "type": "A", "value": [ 1, 2, 3, 4, 5 ] } { "type": "B", "value": [ [ 1, 2, 3, 4, 5 ], [ 6, 7, 8 ] ] } type determines the type of value , which in the first example is Vec<u32> and in the second is Vec<Vec<u32>> . If I represent the above data as follows: enum DataValue { TypeA(Vec<u32>), TypeB(Vec<Vec<u32>>) } struct Data { data_type: String, value: DataValue } How do I implement serde deserialization to properly decode these values? 回答1: You