From 1917b23c27ec941169dbe30d492429ac65043c4f Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Thu, 10 Sep 2020 00:32:42 +0800 Subject: [PATCH] fix: use serde --- Cargo.toml | 2 +- src/util_file.rs | 7 +++---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index bbb40ab..4948d04 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rust_util" -version = "0.6.9" +version = "0.6.10" authors = ["Hatter Jiang "] edition = "2018" description = "Hatter's Rust Util" diff --git a/src/util_file.rs b/src/util_file.rs index 65eb5ac..266d714 100644 --- a/src/util_file.rs +++ b/src/util_file.rs @@ -119,14 +119,13 @@ pub fn find_parents_exists_dir(dir: &str) -> Option { } #[cfg(feature = "use_serde")] -pub fn read_json_config(config: Option, files: &[String]) -> XResult> { +pub fn read_json_config(config: Option, files: &[String]) -> XResult> where T: serde::de::DeserializeOwned { let config_path_buf_opt = read_config(config, files); match config_path_buf_opt { None => Ok(None), Some(config_path_buf) => { - // information!("Read config: {}", config_path_buf); - let config_content = fs::read_to_string(config_path_buf)?; - Ok(Some((config_path_buf, serde_json::from_str(&config_content)?))) + let json_config: T = serde_json::from_reader(std::fs::File::open(&config_path_buf)?)?; + Ok(Some((config_path_buf, json_config))) } } }