lumina-framework 0.1.5

A beautiful, fast, and elegant web framework for Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
use serde::{de::DeserializeOwned, Serialize};

/// Converts any serializable data into a JSON string, returning a default empty JSON string on error.
/// Human-friendly utility similar to `json_encode` in PHP / JavaScript `JSON.stringify`.
pub fn json_encode<T: Serialize>(data: &T) -> String {
    serde_json::to_string(data).unwrap_or_else(|_| "{}".to_string())
}

/// Converts a JSON string into a deserialized Rust struct/type `T`.
/// Human-friendly utility similar to `json_decode` in PHP / JavaScript `JSON.parse`.
pub fn json_decode<T: DeserializeOwned>(json_str: &str) -> Result<T, serde_json::Error> {
    serde_json::from_str(json_str)
}