Skip to main content

scientific_workflow/storage/json_payload_decoder/
vec_f64.rs

1//! JSON decoding for a state field whose concrete payload is `Vec<f64>`.
2//!
3//! [`JsonVecF64Decoder`] converts exactly one raw JSON field value into an owned
4//! vector of double-precision values. The [`StoredStateSeriesReader`](crate::storage::stored_state_series_reader::StoredStateSeriesReader)
5//! remains responsible for finding the field by key, selecting this decoder,
6//! and inserting the returned vector into the matching state slot.
7//!
8//! The expected JSON representation is an array of JSON numbers accepted by
9//! Serde JSON for `f64`, for example `[1.0,-2.5,3.25]`.
10//! Deserialization allocates the final vector directly and does not first build
11//! a [`serde_json::Value`] tree. An empty array is valid. JSON `null`, scalar
12//! values, nested arrays, and non-numeric elements are rejected by Serde.
13//!
14//! This decoder performs no key lookup, record parsing, state mutation,
15//! filesystem access, or domain-specific validation of vector length and
16//! values. Applications needing constraints such as a fixed dimension or a
17//! finite-only vector should register a custom decoder for that key.
18
19use super::JsonPayloadDecoder;
20
21/// Stateless default decoder for payloads stored as `Vec<f64>`.
22///
23/// The unit struct is zero-sized and may be copied freely while configuring
24/// decoder registries. Each registry entry still binds it to exactly one key
25/// and the concrete `Vec<f64>` output type.
26#[derive(Clone, Copy, Debug, Default)]
27pub struct JsonVecF64Decoder;
28
29impl JsonPayloadDecoder<Vec<f64>> for JsonVecF64Decoder {
30    type Error = serde_json::Error;
31
32    /// Deserializes one complete raw JSON array directly into `Vec<f64>`.
33    ///
34    /// The returned vector owns its allocation. On failure, the original
35    /// `serde_json::Error` is returned so the decoder registry can retain it as
36    /// the source of a stream-, index-, and key-aware storage error.
37    fn decode_json_payload(&self, raw_json: &str) -> Result<Vec<f64>, Self::Error> {
38        serde_json::from_str(raw_json)
39    }
40}