From 53cafb5925510dcc40fec2e0b16ac6fa4d012f68 Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Tue, 5 Mar 2019 12:50:52 +0100 Subject: [PATCH] src/tools.rs: impl file_get_json() --- src/tools.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/tools.rs b/src/tools.rs index 1b89b45c..44dd99ff 100644 --- a/src/tools.rs +++ b/src/tools.rs @@ -118,6 +118,19 @@ pub fn file_get_contents>(path: P) -> Result, Error> { }).map_err(|err| format_err!("unable to read {:?} - {}", path, err)) } +pub fn file_get_json>(path: P) -> Result { + + let path = path.as_ref(); + + let raw = file_get_contents(path)?; + + try_block!({ + let data = String::from_utf8(raw)?; + let json = serde_json::from_str(&data)?; + Ok(json) + }).map_err(|err: Error| format_err!("unable to read json from {:?} - {}", path, err)) +} + /// Atomically write a file. We first create a temporary file, which /// is then renamed. pub fn file_set_contents>(