titanium_model/
json.rs

1//! JSON serialization abstraction.
2//!
3//! Provides a unified interface for `simd-json` (default) and `serde_json`.
4
5#[cfg(feature = "simd")]
6pub use simd_json::json;
7#[cfg(feature = "simd")]
8pub use simd_json::BorrowedValue;
9#[cfg(feature = "simd")]
10pub use simd_json::Error;
11#[cfg(feature = "simd")]
12pub use simd_json::OwnedValue as Value;
13
14#[cfg(all(not(feature = "simd"), feature = "serde"))]
15pub use serde_json::json;
16#[cfg(all(not(feature = "simd"), feature = "serde"))]
17pub use serde_json::Error;
18#[cfg(all(not(feature = "simd"), feature = "serde"))]
19pub use serde_json::Value;
20
21use serde::de::DeserializeOwned;
22use serde::{Deserialize, Serialize};
23
24#[cfg(feature = "simd")]
25/// Parse JSON from a string slice.
26///
27/// # Errors
28///
29/// Returns an error if parsing fails.
30pub fn from_str<T: DeserializeOwned>(json: &str) -> Result<T, Error> {
31    // SIMD-JSON requires mutable access. We clone to ensure safety and compatibility
32    // with immutable generic interfaces, though this incurs a copy cost.
33    // Use `from_mut_str` or `from_string` for zero-copy/in-place performance.
34    let mut buffer = json.as_bytes().to_vec();
35    simd_json::from_slice(&mut buffer)
36}
37
38#[cfg(all(not(feature = "simd"), feature = "serde"))]
39/// Parse JSON from a string slice.
40///
41/// # Errors
42///
43/// Returns an error if parsing fails.
44pub fn from_str<T: DeserializeOwned>(json: &str) -> Result<T, Error> {
45    serde_json::from_str(json)
46}
47
48#[cfg(feature = "simd")]
49/// Serialize a value to a JSON string.
50///
51/// # Errors
52///
53/// Returns an error if serialization fails.
54pub fn to_string<T: Serialize>(value: &T) -> Result<String, Error> {
55    simd_json::to_string(value)
56}
57
58#[cfg(all(not(feature = "simd"), feature = "serde"))]
59/// Serialize a value to a JSON string.
60///
61/// # Errors
62///
63/// Returns an error if serialization fails.
64pub fn to_string<T: Serialize>(value: &T) -> Result<String, Error> {
65    serde_json::to_string(value)
66}
67
68#[cfg(feature = "simd")]
69/// Serialize a value to a JSON Value.
70///
71/// # Errors
72///
73/// Returns an error if serialization fails.
74pub fn to_value<T: Serialize>(value: T) -> Result<Value, Error> {
75    let mut bytes = simd_json::to_vec(&value)?;
76    simd_json::from_slice(&mut bytes)
77}
78
79#[cfg(feature = "simd")]
80/// Parse a BorrowedValue from a mutable slice.
81///
82/// # Errors
83///
84/// Returns an error if parsing fails.
85pub fn to_borrowed_value(json: &mut [u8]) -> Result<BorrowedValue<'_>, Error> {
86    simd_json::to_borrowed_value(json)
87}
88
89#[cfg(feature = "simd")]
90/// Deserialize a value from a JSON Value.
91///
92/// # Errors
93///
94/// Returns an error if deserialization fails.
95pub fn from_value<T: DeserializeOwned>(value: Value) -> Result<T, Error> {
96    T::deserialize(value)
97}
98
99#[cfg(feature = "simd")]
100/// Parse JSON from a mutable slice (Zero-Copy).
101///
102/// This modifies the input buffer in-place to avoid allocations.
103///
104/// # Errors
105///
106/// Returns an error if parsing fails.
107pub fn from_slice_mut<'a, T: Deserialize<'a>>(json: &'a mut [u8]) -> Result<T, Error> {
108    simd_json::from_slice(json)
109}
110
111#[cfg(feature = "simd")]
112/// Deserialize from a BorrowedValue.
113///
114/// # Errors
115///
116/// Returns an error if deserialization fails.
117pub fn from_borrowed_value<'a, T: Deserialize<'a>>(value: BorrowedValue<'a>) -> Result<T, Error> {
118    T::deserialize(value)
119}
120
121#[cfg(all(not(feature = "simd"), feature = "serde"))]
122/// Serialize a value to a JSON Value.
123///
124/// # Errors
125///
126/// Returns an error if serialization fails.
127pub fn to_value<T: Serialize>(value: T) -> Result<Value, Error> {
128    serde_json::to_value(value)
129}
130
131#[cfg(all(not(feature = "simd"), feature = "serde"))]
132/// Parse JSON from a mutable slice.
133///
134/// # Errors
135///
136/// Returns an error if parsing fails.
137pub fn from_slice_mut<T: DeserializeOwned>(json: &mut [u8]) -> Result<T, Error> {
138    serde_json::from_slice(json)
139}