Skip to content

How to serialize and Deserialize closure? #24

@TrionProg

Description

@TrionProg

Hello, I am trying to do it.

You wrote:

you either need to specify the precise type you're deserializing without naming it (which is possible but not particularly practical)

How to do it(to deserialize)?

I use serde_traitobject and write this code

use serde_traitobject as st;

let one:usize = 1;
let two:usize = 2;
let plus_one = Fn!(|x: usize| x + one + two);

let s: st::Box<dyn serde_traitobject::Fn(usize) -> usize> = st::Box::new(plus_one);

let serialized = serde_json::to_string(&s).unwrap();
println!("{}",serialized);

let deserialized: st::Box<dyn serde_traitobject::Fn(usize) -> usize> = serde_json::from_str(serialized.as_str()).unwrap();

And I have error:
the trait bound `&usize: main::_IMPL_DESERIALIZE_FOR_MyStruct::_serde::Deserialize<'_>` is not satisfied

What should I do?

Can I create struct with two or more closures and serialize and deserialize it? Without Box<dyn ... Fn> just Box?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions