In this recipe, we use the serde_json crate that provides us with a from_str function that allows the developer to parse &str of JSON into a type of the caller's choice. We assign a string format of JSON to the j variable, after which we call serde_json::from_str(j) and assign the output to a variable named parsed. The unstructured JSON is parsed into a universal serde_json::Value type that represents any valid JSON data.
We then compare, with the assert_eq! macro, the value of the parsed &str of JSON with what we expect the parsed value to be. The expected value is declared using the json! macro.
The error-chain crate is a library for consistent and reliable error handling that makes it easier to take full advantage of Rust's powerful error-handling features, without the overhead of maintaining boilerplate error types and conversions. It implements a strategy for defining your own error types, as well as conversions from others' error types.
The basic pattern we use here has a function named run() that produces a Result that acts like a real main function. We use the error-chain crate to make ? work within run. This is using the error_chain! macro from the error-chain to define a custom Error and Result type, along with automatic conversions from the crate error types. The automatic conversions make the ? operator work. The quick_main! macro generates the actual main function and prints out the error if it occurs during the course of execution.
We return Ok(()) to the quick_run! macro to ensure that the program executed successfully without any errors.