DeserializeFromJsonReader
Read a value from any serde_json reader through the context, and reject anything after it.
Overview
In cgp-serde, a serialization library built with
Context-Generic Programming (CGP), DeserializeFromJsonReader makes reading JSON an
operation the context, the type whose wiring holds an application's choices, runs through CGP's
TryComputer. It reads from any of
serde_json's input sources: a string, a byte slice, or a stream. It is keyed on the marker type
DeserializeJson<T>, from cgp_serde_json::code, which names the type to read.
Definition
#[cgp_impl(new DeserializeFromJsonReader)]
#[use_type(HasErrorType.Error)]
#[uses(CanDeserializeValue<'de, Value>, CanRaiseError<serde_json::Error>)]
impl<Value, R, 'de> TryComputer<DeserializeJson<Value>, R>
where
R: Read<'de>,
{
type Output = Value;
fn try_compute(
&self,
_code: PhantomData<DeserializeJson<Value>>,
source: R,
) -> Result<Value, Error> { ... }
}
Read is serde_json::de::Read, implemented by serde_json's StrRead, SliceRead, and
IoRead. The marker types are defined as:
pub struct DeserializeJson<T>(pub PhantomData<T>);
pub struct SerializeJson;
Usage
Import it from cgp_serde_json::providers, and wire it under TryComputerComponent for every
target type with one generic entry:
@TryComputerComponent.<T> DeserializeJson<T>:
DeserializeFromJsonReader,
A caller passes a reader:
let payload: Payload = app
.try_compute(PhantomData::<DeserializeJson<Payload>>, StrRead::new(&text))?;
Behavior
The provider builds a serde_json::Deserializer over the reader, reads the value through the
context, and then checks that nothing follows it, so "a" x fails with
trailing characters at line 1 column 5. Every serde_json error is turned into the context's
error type with raise_error.
The reader's lifetime flows through to the value's deserializer, so a reader over a string or a byte
slice can produce a value that borrows from the input, such as a &str. A stream cannot lend its
input, so a value that borrows from it cannot be read from a stream, as with plain serde_json.
Context dependencies
CanDeserializeValue<'de, Value> for the target type, and an error type that can be raised from a
serde_json::Error: HasErrorType and CanRaiseError<serde_json::Error>.
Pairing
SerializeToJsonString writes JSON.
When to use it
Reach for DeserializeFromJsonReader to read JSON from bytes or a stream, or to produce a value
that borrows from its input. For a string,
DeserializeFromJsonString takes the string directly and wraps
it in a reader itself.
Related constructs
DeserializeWithContext, the seed the provider drives.CanDeserializeJsonString, a method for reading a string with noTryComputerentry.
The ideas behind it
- The bridge to Serde: why reading needs a seed and a format's own deserializer.
- Modular error handling: raising into the context's error type.
Source
An AI agent wrote this page using the CGP knowledge base. Its content was verified against the project's source. See How AI is used in this project.