JsonResult
JsonResult is a Rust generic enum designed for seamlessly handling JSON values that can represent either a success (Ok) or error (Err) result, with flexible types. It integrates tightly with Serde for serialization and deserialization.
Features
- Supports untagged enum representation for natural JSON parsing.
- Converts to and from
serde_json::Valueeasily. - Provides detailed error messages when deserialization fails.
- Generic over success (
T) and error (E) types.
Installation
Add this to your Cargo.toml:
[]
= { = "1.0", = ["derive"] }
= "1.0"
Usage
use json;
use TryFrom;
let success_json = json!;
let error_json = json!;
// Define your success and error types
type MyJsonResult = ;
// Deserialize JSON into JsonResult
let res_ok: MyJsonResult = success_json.try_into.unwrap;
let res_err: MyJsonResult = error_json.try_into.unwrap;
match res_ok
match res_err
// Convert JsonResult back to serde_json::Value
let json_val: Value = res_ok.into;
println!;