The trait `serde::Deserializer<'_>` is not implemented for `tests::TestReq`

Hello I tried to create a function to deserializa a object but I get this error:

the trait serde::Deserializer<'_>is not implemented fortests::TestReq`

and I have the macro deserialize

#[derive(Serialize, Deserialize, Debug)]
        struct TestHeader {
            #[serde(rename = "Content-Type")]
            content_type: String,
            #[serde(rename = "Authorization")]
            pub authorization: String,
        }

    #[derive(Serialize, Deserialize, Debug)]
    struct Body {
        user: String,
    }

    #[derive(Serialize, Deserialize, Debug)]
    struct TestReq {
        headers: TestHeader,
        method: utils::Methods,
        body: Body,
}

this is the fuction:

fn deserialize_network<'a>(req: TestReq) -> &'a[u8] {
let des_req: &[u8] = serde::de::Deserialize::deserialize(req).unwrap();

        des_req

}

and this is the source

https://github.com/spielrs/malaga-http_utils

What you are trying to achieve? Deserializer is used to create object from serialized string, not something from existing object.

Maybe I don't know how works deserialize. can I get the object serialized directly and turn it in binary again?

Of course. This is created for just this case - to have the object seriaized into some other format (either human-readable, or, for example, suitable to be sent over Internet), treated as string or byte sequence, and then deserialized back from this sequence into the object in memory. The simplest case is probably JSON, where serialized data is just a text, so the deserializer will take the &str.

1 Like

I think that I have understood it. To serialize to binary I have used bincode, but it seems like doesn't work fine because only deserialize one part of the binary

this is the test

fn it_should_serializa_to_binary() {
        let resq = b"{
            \"headers\": {
                \"Content-Type\": \"application/json\",
                \"Authorization\": \"Basis 1ddmcdd\"
            },
            \"method\": \"POST\",
            \"body\": {
                \"user\":\"test\"
            }
        }";

        let resq_struct_ser: TestReq = TestReq::serialize_to_struct(resq);

        println!("struct: {:#?}", resq_struct_ser);

        let resq_binary_ser: Vec<u8> = TestReq::serialize_to_binary(resq_struct_ser);

        let req_string: String = bincode::deserialize(&resq_binary_ser[..]).unwrap();
        println!("binary {:#?}", req_string);
    }

the implementation:

impl NetworkObj for TestReq {
        fn serialize_to_struct(req: &[u8]) -> Self {
            let req_string = str::from_utf8(req).unwrap();
            let ser_req: TestReq = serde_json::from_str(req_string).unwrap();
            
            ser_req
        }

        fn serialize_to_binary(req: TestReq) -> Vec<u8> {
            let ser_req: Vec<u8> = bincode::serialize(&req).unwrap();

            ser_req
        } 
    }

and the result is

running 2 tests
struct: TestReq {
    headers: TestHeader {
        content_type: "application/json",
        authorization: "Basis 1ddmcdd"
    },
    method: POST,
    body: Body {
        user: "test"
    }
}
test tests::it_should_serialize_to_strcut ... ok
binary "application/json"
test tests::it_should_serializa_to_binary ... ok

test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

   Doc-tests malaga_http_utils

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

it only serialize application/json

You're only asking it to deserialize a single String, not an entire TestReq.

1 Like

you rigth, this works:

let req_string: TestReq = bincode::deserialize(&amp;resq_binary_ser[..]).unwrap();