Skip to main content

JsonCodec

Trait JsonCodec 

Source
pub trait JsonCodec<T>: Send + Sync {
    // Required methods
    fn encode(&self, value: &T) -> Option<Value>;
    fn decode(&self, value: &Value) -> Option<T>;

    // Provided methods
    fn encode_to_vec(&self, value: &T) -> Option<Vec<u8>> { ... }
    fn decode_slice(&self, bytes: &[u8]) -> Option<T> { ... }
}
Expand description

Type-erased JSON codec for one record type.

Stored as Arc<dyn JsonCodec<T>> inside TypedRecord<T, R>, where the blanket AnyRecord impl cannot carry a T: RemoteSerialize bound (it must also cover non-serializable record types). T is fixed per record, so the trait is object-safe.

Required Methods§

Source

fn encode(&self, value: &T) -> Option<Value>

Encode a typed value to JSON, or None on failure.

Source

fn decode(&self, value: &Value) -> Option<T>

Decode a JSON value into T, or None on schema mismatch.

Provided Methods§

Source

fn encode_to_vec(&self, value: &T) -> Option<Vec<u8>>

Encode a typed value into owned JSON bytes.

Defaulted through encode so existing custom codecs remain source-compatible and retain their exact JSON-tree semantics.

Source

fn decode_slice(&self, bytes: &[u8]) -> Option<T>

Decode one complete JSON value from a byte slice.

Defaulted through decode so existing custom codecs remain source-compatible.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§