qubit_codec/value/json_bytes_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 bytes 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_slice;
15
16use super::ValueDecoder;
17
18/// Decodes JSON bytes into one 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::{JsonBytesValueDecoder, ValueDecoder};
30/// use serde::Deserialize;
31///
32/// #[derive(Debug, Deserialize, PartialEq)]
33/// struct Point {
34/// x: i32,
35/// y: i32,
36/// }
37///
38/// let mut decoder = JsonBytesValueDecoder::<Point>::new();
39/// let point = decoder.decode(br#"{"x":1,"y":2}"#).unwrap();
40/// assert_eq!(point, Point { x: 1, y: 2 });
41/// ```
42#[must_use]
43#[derive(Debug)]
44pub struct JsonBytesValueDecoder<T> {
45 /// Associates the decoder with `T` without retaining a value.
46 _marker: PhantomData<T>,
47}
48
49impl<T> Copy for JsonBytesValueDecoder<T> {}
50
51impl<T> Clone for JsonBytesValueDecoder<T> {
52 /// Returns an identical stateless decoder without requiring `T: Clone`.
53 ///
54 /// # Returns
55 ///
56 /// Returns a decoder associated with the same target type.
57 #[inline]
58 fn clone(&self) -> Self {
59 *self
60 }
61}
62
63impl<T> Default for JsonBytesValueDecoder<T> {
64 /// Creates a decoder for `T` without constructing a value of that type.
65 ///
66 /// # Returns
67 ///
68 /// Returns an uninitialized stateless decoder associated with `T`.
69 #[inline]
70 fn default() -> Self {
71 Self::new()
72 }
73}
74
75impl<T> JsonBytesValueDecoder<T> {
76 /// Creates a JSON bytes decoder for values of type `T`.
77 ///
78 /// # Returns
79 ///
80 /// Returns a stateless decoder without allocating or constructing a `T`.
81 #[inline]
82 pub const fn new() -> Self {
83 Self { _marker: PhantomData }
84 }
85}
86
87impl<T> ValueDecoder<[u8]> for JsonBytesValueDecoder<T>
88where
89 T: DeserializeOwned,
90{
91 /// JSON syntax or target-value deserialization failure.
92 type Error = JsonError;
93 /// Owned value deserialized independently of the input byte slice.
94 type Output = T;
95
96 /// Deserializes `input` using [`serde_json::from_slice`].
97 ///
98 /// # Parameters
99 ///
100 /// - `input`: One complete JSON value, optionally surrounded by whitespace.
101 /// String-content validation follows `T`'s Serde deserialization
102 /// implementation.
103 ///
104 /// # Returns
105 ///
106 /// Returns an owned `T`; decoding may allocate according to the target
107 /// type.
108 ///
109 /// # Errors
110 ///
111 /// Returns a JSON error for malformed or incomplete input, trailing
112 /// non-whitespace data, or a value incompatible with `T`. Validation
113 /// follows serde_json and `T`'s `Deserialize` implementation; byte-oriented
114 /// targets may accept non-UTF-8 string contents.
115 #[inline]
116 fn decode(&mut self, input: &[u8]) -> Result<Self::Output, Self::Error> {
117 from_slice(input)
118 }
119}