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(any(feature = "std", 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#[cfg(feature = "alloc")]
20mod maps;
21
22pub use float::JsonF64;
23
24/// An item which can be deserialized from a `Value`.
25///
26/// This will deserialize the object present without limitation. This should be kept in mind when
27/// deserializing into types which allocate.
28pub trait JsonDeserialize: Sized {
29  /// Decode this item from a `Value`.
30  fn deserialize<'read, 'parent, B: Read<'read>, S: Stack>(
31    value: Value<'read, 'parent, B, S>,
32  ) -> Result<Self, JsonError<'read, B, S>>;
33}
34
35/// A structure which can deserialized from a JSON serialization.
36pub trait JsonStructure: JsonDeserialize {
37  /// Deserialize this structure from an JSON-serialized blob.
38  ///
39  /// This will deserialize the structure present without limitation. If a bound is desired, bound
40  /// the length of input or deserialize into types which define bounds.
41  ///
42  /// This method SHOULD NOT be overriden.
43  fn deserialize_structure<'read, B: Read<'read>, S: Stack>(
44    json: B,
45  ) -> Result<Self, JsonError<'read, B, S>> {
46    let mut json = Deserializer::new(json)?;
47    let value = json.value()?;
48    Self::deserialize(value)
49  }
50}
51
52/// An item which can be serialized as JSON.
53pub trait JsonSerialize {
54  /// Serialize this item as JSON.
55  ///
56  /// This returns an `impl Iterator<Item = char>` to maintain support for serializing without
57  /// requiring an allocator.
58  fn serialize(&self) -> impl Iterator<Item = char>;
59}