Expand description
A small crate to manipulate Json data.
§Features
- Categorizes
serde_json::Errorinto specific error types (Syntax,IO,Data,EOF) - Each error type is associated with a custom code, HTTP status, and descriptive message
- Structured output for APIs, logging systems, and observability platforms
- Includes context metadata via
BTreeMap - Provides a convenient
convert_json_result!macro for error conversion
§Usage
Using the JsonErrorConverter directly:
use cdumay_core::{Error, ErrorConverter};
use serde_json::Value;
use std::collections::BTreeMap;
use cdumay_json::JsonErrorConverter;
fn parse_json(input: &str) -> cdumay_core::Result<Value> {
serde_json::from_str::<Value>(input).map_err(|e| {
let mut ctx = BTreeMap::new();
ctx.insert("input".to_string(), serde_value::Value::String(input.to_string()));
JsonErrorConverter::convert(&e, "Failed to parse JSON".to_string(), ctx)
})
}Using the convert_json_result! macro:
use cdumay_json::convert_json_result;
use serde_json::Value;
use std::collections::BTreeMap;
use cdumay_core::{Error, ErrorConverter};
fn parse_json(input: &str) -> cdumay_core::Result<Value> {
// Basic usage with just the result
convert_json_result!(serde_json::from_str::<Value>(input));
// With custom context
let mut ctx = BTreeMap::new();
ctx.insert("input".to_string(), serde_value::Value::String(input.to_string()));
convert_json_result!(serde_json::from_str::<Value>(input), ctx.clone());
// With custom context and message
convert_json_result!(serde_json::from_str::<Value>(input), ctx, "Failed to parse JSON")
}Macros§
- convert_
json_ result - Macro to convert a
Result<T, serde_json::Error>into acdumay_core::Result<T>
Structs§
- Data
Error - Error : DataError (Kind:
JsonData) - EofError
- Error : EofError (Kind:
JsonEof) - IoError
- Error : IoError (Kind:
JsonIo) - Json
Error Converter - A utility struct for handling JSON errors and converting them into standardized error types.
- Syntax
Error - Error : SyntaxError (Kind:
JsonSyntax)
Constants§
- Json
Data - ErrorKind : JsonData (400) - Invalid JSON data
- JsonEof
- ErrorKind : JsonEof (500) - Reached the end of the input data
- JsonIo
- ErrorKind : JsonIo (500) - IO Error
- Json
Syntax - ErrorKind : JsonSyntax (400) - Syntax Error