1#![cfg_attr(docsrs, feature(doc_cfg))]
2#![doc = include_str!("../README.md")]
3#![deny(missing_docs)]
4#![no_std]
5
6#[cfg(feature = "alloc")]
7extern crate alloc;
8
9pub use core_json::*;
10
11mod primitives;
12mod sequences;
13
14pub trait JsonDeserialize: Sized {
19 fn deserialize<'bytes, 'parent, B: BytesLike<'bytes>, S: Stack>(
21 value: Value<'bytes, 'parent, B, S>,
22 ) -> Result<Self, JsonError<'bytes, B, S>>;
23}
24
25pub trait JsonStructure: JsonDeserialize {
27 fn deserialize_structure<'bytes, B: BytesLike<'bytes>, S: Stack>(
34 json: B,
35 ) -> Result<Self, JsonError<'bytes, B, S>> {
36 let mut json = Deserializer::new(json)?;
37 let value = json.value()?;
38 Self::deserialize(value)
39 }
40}
41
42impl<T: JsonDeserialize> JsonDeserialize for Option<T> {
43 fn deserialize<'bytes, 'parent, B: BytesLike<'bytes>, S: Stack>(
45 value: Value<'bytes, 'parent, B, S>,
46 ) -> Result<Self, JsonError<'bytes, B, S>> {
47 if value.is_null()? {
48 return Ok(None);
49 }
50 T::deserialize(value).map(Some)
51 }
52}
53
54#[cfg(feature = "alloc")]
55use alloc::string::String;
56#[cfg(feature = "alloc")]
57impl JsonDeserialize for String {
58 fn deserialize<'bytes, 'parent, B: BytesLike<'bytes>, S: Stack>(
59 value: Value<'bytes, 'parent, B, S>,
60 ) -> Result<Self, JsonError<'bytes, B, S>> {
61 value.to_str()?.collect()
62 }
63}