pub struct JsonEncoder<'budget, R = JsonResource, Q = usize>where
Q: ResourceQuantity,{ /* private fields */ }Expand description
Encodes strict JSON text while owning cumulative accounting state.
Signed and unsigned 64-bit integers are supported in full. Serde i128
values are accepted only when they fit i64, or are non-negative and fit
u64; u128 values must fit u64. Wider integers return a serialization
error instead of being truncated or converted to strings. Floating-point
values must be finite.
§Type Parameters
R- Resource identity tracked by the encode session.Q- Quantity representation used for resource accounting.
§Examples
use qubit_json::encode::JsonEncoder;
let mut encoder = JsonEncoder::unlimited();
let bytes = encoder.to_vec(&serde_json::json!({"ok": true}))?;
assert_eq!(bytes, br#"{"ok":true}"#);Implementations§
Source§impl<R, Q> JsonEncoder<'static, R, Q>
impl<R, Q> JsonEncoder<'static, R, Q>
Sourcepub fn with_limits(limits: JsonEncodeLimits<R, Q>) -> Self
pub fn with_limits(limits: JsonEncodeLimits<R, Q>) -> Self
Source§impl JsonEncoder<'static, JsonResource, usize>
impl JsonEncoder<'static, JsonResource, usize>
Source§impl<'budget, R, Q> JsonEncoder<'budget, R, Q>
impl<'budget, R, Q> JsonEncoder<'budget, R, Q>
Sourcepub fn new(session: JsonEncodeSession<'budget, R, Q>) -> Self
pub fn new(session: JsonEncodeSession<'budget, R, Q>) -> Self
Creates an encoder that owns a reusable cumulative session.
§Parameters
session- Session that receives committed output accounting.
§Returns
An encoder that retains session until Self::into_session is called
or the encoder is dropped.
Sourcepub const fn session(&self) -> &JsonEncodeSession<'budget, R, Q>
pub const fn session(&self) -> &JsonEncodeSession<'budget, R, Q>
Returns the cumulative session for read-only inspection.
The reference exposes charges committed by completed encode operations and remains borrowed from this encoder.
§Returns
A shared reference to the cumulative encode session.
Sourcepub const fn session_mut(&mut self) -> &mut JsonEncodeSession<'budget, R, Q>
pub const fn session_mut(&mut self) -> &mut JsonEncodeSession<'budget, R, Q>
Returns mutable access to the cumulative session.
Changes made through the reference affect the limits and accounting state used by subsequent encode operations.
§Returns
A mutable reference to the cumulative encode session.
Sourcepub fn into_session(self) -> JsonEncodeSession<'budget, R, Q>
pub fn into_session(self) -> JsonEncodeSession<'budget, R, Q>
Returns the cumulative session and consumes the encoder.
No output is produced and no accounting is reset; ownership of the accumulated state is transferred to the caller.
§Returns
The session previously owned by this encoder.
Sourcepub fn to_vec<T>(&mut self, value: &T) -> Result<Vec<u8>, JsonEncodeError<R, Q>>
pub fn to_vec<T>(&mut self, value: &T) -> Result<Vec<u8>, JsonEncodeError<R, Q>>
Encodes value into compact JSON and commits only complete success.
§Type Parameters
T- Source type serialized into JSON.
§Parameters
value- Value to serialize.
§Returns
The compact JSON bytes on success.
§Errors
Returns JsonEncodeErrorKind::Budget when accounting rejects the value
or output, or a serialization error when Serde rejects value.
Sourcepub fn write_buffered<W, T>(
&mut self,
writer: W,
value: &T,
) -> Result<(), JsonEncodeError<R, Q>>
pub fn write_buffered<W, T>( &mut self, writer: W, value: &T, ) -> Result<(), JsonEncodeError<R, Q>>
Buffers a complete document before writing it to writer.
§Type Parameters
W- Destination writer type.T- Source type serialized into JSON.
§Parameters
writer- Destination receiving the complete JSON document.value- Value to serialize.
§Returns
Ok(()) after the complete document is written and accounting is
committed.
§Errors
Returns JsonEncodeErrorKind::Budget when accounting rejects the value
or output, or a serialization/writer error on failure.
Sourcepub fn write_incremental<W, T>(
&mut self,
writer: W,
value: &T,
) -> Result<(), JsonEncodeError<R, Q>>
pub fn write_incremental<W, T>( &mut self, writer: W, value: &T, ) -> Result<(), JsonEncodeError<R, Q>>
Streams value directly to writer, retaining accepted prefixes.
§Type Parameters
W- Destination writer type.T- Source type serialized into JSON.
§Parameters
writer- Destination receiving streamed JSON bytes.value- Value to serialize.
§Returns
Ok(()) after serialization and output accounting complete.
§Errors
Returns JsonEncodeErrorKind::Budget when accounting rejects output,
or a serialization/writer error on failure. Accepted output prefixes
remain written when a later operation fails.