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 UTF-8 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, UTF-8, 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` from UTF-8 JSON bytes.
97 ///
98 /// # Parameters
99 ///
100 /// - `input`: One complete UTF-8 JSON value, optionally surrounded by
101 /// whitespace.
102 ///
103 /// # Returns
104 ///
105 /// Returns an owned `T`; decoding may allocate according to the target
106 /// type.
107 ///
108 /// # Errors
109 ///
110 /// Returns a JSON error for invalid UTF-8, malformed or incomplete input,
111 /// trailing non-whitespace data, or a value incompatible with `T`.
112 #[inline]
113 fn decode(&mut self, input: &[u8]) -> Result<Self::Output, Self::Error> {
114 from_slice(input)
115 }
116}