core_json_traits/
lib.rs

1#![cfg_attr(docsrs, feature(doc_cfg))]
2#![doc = include_str!("../README.md")]
3#![deny(missing_docs)]
4#![cfg_attr(not(test), no_std)]
5
6#[cfg(feature = "alloc")]
7extern crate alloc;
8
9pub use core_json::*;
10
11mod primitives;
12mod float;
13mod option;
14mod sequences;
15mod string;
16
17#[cfg(feature = "alloc")]
18mod boxed;
19
20pub use float::JsonF64;
21
22/// An item which can be deserialized from a `Value`.
23///
24/// This will deserialize the object present without limitation. This should be kept in mind when
25/// deserializing into types which allocate.
26pub trait JsonDeserialize: Sized {
27  /// Decode this item from a `Value`.
28  fn deserialize<'read, 'parent, B: Read<'read>, S: Stack>(
29    value: Value<'read, 'parent, B, S>,
30  ) -> Result<Self, JsonError<'read, B, S>>;
31}
32
33/// A structure which can deserialized from a JSON serialization.
34pub trait JsonStructure: JsonDeserialize {
35  /// Deserialize this structure from an JSON-serialized blob.
36  ///
37  /// This will deserialize the structure present without limitation. If a bound is desired, bound
38  /// the length of input or deserialize into types which define bounds.
39  ///
40  /// This method SHOULD NOT be overriden.
41  fn deserialize_structure<'read, B: Read<'read>, S: Stack>(
42    json: B,
43  ) -> Result<Self, JsonError<'read, B, S>> {
44    let mut json = Deserializer::new(json)?;
45    let value = json.value()?;
46    Self::deserialize(value)
47  }
48}
49
50/// An item which can be serialized as JSON.
51pub trait JsonSerialize {
52  /// Serialize this item as JSON.
53  ///
54  /// This returns an `impl Iterator<Item = char>` to maintain support for serializing without
55  /// requiring an allocator.
56  fn serialize(&self) -> impl Iterator<Item = char>;
57}