core_json_traits/
sequences.rs

1use crate::{BytesLike, Stack, JsonError, Value, JsonDeserialize, JsonStructure};
2
3impl<T: 'static + Default + JsonDeserialize, const N: usize> JsonDeserialize for [T; N] {
4  fn deserialize<'bytes, 'parent, B: BytesLike<'bytes>, S: Stack>(
5    value: Value<'bytes, 'parent, B, S>,
6  ) -> Result<Self, JsonError<'bytes, B, S>> {
7    let mut res: Self = core::array::from_fn(|_| Default::default());
8    let mut iter = value.iterate()?;
9    let mut i = 0;
10    while let Some(item) = iter.next() {
11      if i == N {
12        Err(JsonError::TypeError)?;
13      }
14      res[i] = T::deserialize(item?)?;
15      i += 1;
16    }
17    if i != N {
18      Err(JsonError::TypeError)?;
19    }
20    Ok(res)
21  }
22}
23impl<T: 'static + Default + JsonDeserialize, const N: usize> JsonStructure for [T; N] {}
24
25#[cfg(feature = "alloc")]
26use alloc::{vec, vec::Vec};
27#[cfg(feature = "alloc")]
28impl<T: 'static + JsonDeserialize> JsonDeserialize for Vec<T> {
29  fn deserialize<'bytes, 'parent, B: BytesLike<'bytes>, S: Stack>(
30    value: Value<'bytes, 'parent, B, S>,
31  ) -> Result<Self, JsonError<'bytes, B, S>> {
32    let mut res = vec![];
33    let mut iter = value.iterate()?;
34    while let Some(item) = iter.next() {
35      res.push(T::deserialize(item?)?);
36    }
37    Ok(res)
38  }
39}
40#[cfg(feature = "alloc")]
41impl<T: 'static + JsonDeserialize> JsonStructure for Vec<T> {}