qubit_codec/value/json_string_value_decoder.rs
1// =============================================================================
2// Copyright (c) 2026 Haixing Hu.
3//
4// SPDX-License-Identifier: Apache-2.0
5//
6// Licensed under the Apache License, Version 2.0.
7// =============================================================================
8//! Serde JSON string decoder for one owned value.
9
10use core::marker::PhantomData;
11
12use serde::de::DeserializeOwned;
13use serde_json::Error as JsonError;
14use serde_json::from_str;
15
16use super::ValueDecoder;
17
18/// Decodes one JSON string into an owned value via `serde_json`.
19/// Its stateless marker implements `Clone`, `Copy`, and `Default` without
20/// requiring those traits on `T`.
21///
22/// # Type Parameters
23///
24/// - `T`: Target value type deserialized through Serde.
25///
26/// # Examples
27///
28/// ```rust
29/// use qubit_codec::JsonStringValueDecoder;
30/// use qubit_codec::ValueDecoder;
31/// use serde::Deserialize;
32///
33/// #[derive(Debug, Deserialize, PartialEq)]
34/// struct Point {
35/// x: i32,
36/// y: i32,
37/// }
38///
39/// let mut decoder = JsonStringValueDecoder::<Point>::new();
40/// let point = decoder.decode(r#"{"x":1,"y":2}"#).unwrap();
41/// assert_eq!(point, Point { x: 1, y: 2 });
42/// ```
43#[must_use]
44#[derive(Debug)]
45pub struct JsonStringValueDecoder<T> {
46 /// Tracks the owned output type without storing a value between calls.
47 _marker: PhantomData<T>,
48}
49
50impl<T> Copy for JsonStringValueDecoder<T> {}
51
52impl<T> Clone for JsonStringValueDecoder<T> {
53 /// Copies the stateless decoder marker without requiring `T: Clone`.
54 #[inline]
55 fn clone(&self) -> Self {
56 *self
57 }
58}
59
60impl<T> Default for JsonStringValueDecoder<T> {
61 /// Creates the stateless decoder through its const constructor.
62 #[inline]
63 fn default() -> Self {
64 Self::new()
65 }
66}
67
68impl<T> JsonStringValueDecoder<T> {
69 /// Creates a JSON string decoder for values of type `T`.
70 ///
71 /// # Returns
72 ///
73 /// Returns a stateless decoder without allocating or constructing a `T`.
74 #[inline]
75 pub const fn new() -> Self {
76 Self { _marker: PhantomData }
77 }
78}
79
80impl<T> ValueDecoder<str> for JsonStringValueDecoder<T>
81where
82 T: DeserializeOwned,
83{
84 /// JSON syntax or target-value deserialization failure.
85 type Error = JsonError;
86 /// Owned value deserialized independently of the input string lifetime.
87 type Output = T;
88
89 /// Deserializes `input` from a JSON string.
90 ///
91 /// # Parameters
92 ///
93 /// - `input`: One complete JSON value, optionally surrounded by whitespace.
94 ///
95 /// # Returns
96 ///
97 /// Returns an owned `T`; decoding may allocate according to the target
98 /// type.
99 ///
100 /// # Errors
101 ///
102 /// Returns a JSON error for malformed or incomplete input, trailing
103 /// non-whitespace data, or a value incompatible with `T`.
104 #[inline]
105 fn decode(&mut self, input: &str) -> Result<Self::Output, Self::Error> {
106 from_str(input)
107 }
108}