serde-json

“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 to build json arrays or objects dynamically with serde_json?

我的梦境 提交于 2021-01-29 12:30:51
问题 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 manage to make something work, but it seems too convoluted to be the right way. Can anyone post a working example? 回答1: You can do this with serde_json::Value : use serde_json::{Map, Value}; let mut map = Map::new(); // assuming keys_vals is a Vec<(String,

How to set desired return type in match structure?

帅比萌擦擦* 提交于 2021-01-07 02:09:34
问题 In the example in the crate documentation of serde_json (parse JSON into a Rust struct), error handling is omitted: use serde::{Deserialize, Serialize}; use serde_json::Result; #[derive(Serialize, Deserialize)] struct Person { name: String, age: u8, phones: Vec<String>, } fn typed_example() -> Result<()> { // Some JSON input data as a &str. Maybe this comes from the user. let data = r#" { "name": "John Doe", "age": 43, "phones": [ "+44 1234567", "+44 2345678" ] }"#; // Parse the string of

Safest way to make a serde_json result globally avaialable [duplicate]

痴心易碎 提交于 2020-12-26 05:19:18
问题 This question already has answers here : How do I create a global, mutable singleton? (3 answers) Is it possible to use global variables in Rust? (5 answers) Closed 18 days ago . What is a safe way to make a serde_json result globally available in rust? For instance I have a pub function in a module that contains let contents = fs::read_to_string(json_file_path).expect("file not found"); let myVar: Foo = match serde_json::from_str(&contents){ ...} println!("WIDTH:{}", myVar.Output.width);

Rust: Deserialize a JSON array into a very simple custom table

纵然是瞬间 提交于 2020-06-28 05:50:06
问题 I'm trying to deserialize an array of arrays (represents a table of string cells) into a custom struct in Rust with serde_json. I know that using serde_json::Value is enough for this simple case but I would like to construct a custom type. use serde::{Deserialize}; use serde_json::{self, Result}; #[derive(Deserialize, Debug)] pub struct Row { pub cells: Vec<String>, } #[derive(Deserialize, Debug)] pub struct Table { pub rows: Vec<Row>, } impl Table { pub fn new(data: &str) -> Result<Table> {

How can I deserialize an enum when the case doesn't match?

ぐ巨炮叔叔 提交于 2020-06-08 19:20:25
问题 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, Deserialize)] enum ResultEvent { Started, Failed, Ok, } #[derive(Debug, Deserialize)] struct JsonResult { #[serde(rename(deserialize = "type"))] test_type: ResultType, event: ResultEvent, test_count: Option<u32>, } I can't find a way to make serde_json use the correct case

What's the difference between using the return statement and omitting the semicolon in Rust?

泄露秘密 提交于 2020-03-24 18:13:52
问题 I'm writing a function that returns a serde_json::Value upon success (and failure). Previously in Rust I have been omitting the semicolon to return data from a function, like in the code example below: use serde_json::{Result, Value}; use core::result::Result as ResultCore; fn returning_function() -> ResultCore<Value, Value> { let data = r#" { "status": "ok", "response": { "data": "secret message!" } } "#; match str_to_json(data) { Ok(json_data) => match json_data["status"].as_str() { Some