apalis_core/codec/
json.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
use std::marker::PhantomData;

use crate::codec::Codec;
use serde::{Deserialize, Serialize};
use serde_json::Value;

/// Json encoding and decoding
#[derive(Debug, Clone, Default)]
pub struct JsonCodec<Output> {
    _o: PhantomData<Output>,
}

impl Codec for JsonCodec<Vec<u8>> {
    type Compact = Vec<u8>;
    type Error = serde_json::Error;
    fn encode<T: Serialize>(input: T) -> Result<Vec<u8>, Self::Error> {
        serde_json::to_vec(&input)
    }

    fn decode<O>(compact: Vec<u8>) -> Result<O, Self::Error>
    where
        O: for<'de> Deserialize<'de>,
    {
        serde_json::from_slice(&compact)
    }
}

impl Codec for JsonCodec<String> {
    type Compact = String;
    type Error = serde_json::Error;
    fn encode<T: Serialize>(input: T) -> Result<String, Self::Error> {
        serde_json::to_string(&input)
    }

    fn decode<O>(compact: String) -> Result<O, Self::Error>
    where
        O: for<'de> Deserialize<'de>,
    {
        serde_json::from_str(&compact)
    }
}

impl Codec for JsonCodec<Value> {
    type Compact = Value;
    type Error = serde_json::Error;
    fn encode<T: Serialize>(input: T) -> Result<Value, Self::Error> {
        serde_json::to_value(input)
    }

    fn decode<O>(compact: Value) -> Result<O, Self::Error>
    where
        O: for<'de> Deserialize<'de>,
    {
        serde_json::from_value(compact)
    }
}