Skip to content

Dependency injection in serializer #627

Description

@celian-garcia

Hello,
I'm currently using your library with great pleasure but I was wondering if I could inject a service as a dependency to the adl_serializer and how to do that.

I saw that a lot of things are done at compile time so maybe that I'll have to use it as a static service ?
This option would work but if you have a better one, I'm listening :)

To illustrate, here is an example with both solutions.

namespace nlohmann {
    template<>
    struct adl_serializer<firefly::Task> {
        static firefly::Task from_json(const json &j) {
            return firefly::Task(
                    j["name"].get<std::string>(),
                    j["description"].get<std::string>(),
                    ////////////////////////////////////////
                    // Solution 1 (wanted one) : Injected instantiated service ? How to inject it ? 
                    service.getProcessingTypeById(j["type"].get<int>()),
                    service.getModuleById(j["module"].get<int>()),
                    // Solution 2 (working) : Use the service statically
                    firefly::DataService::getProcessingTypeById(j["type"].get<int>()),
                    firefly::DataService::getModuleById(j["module"].get<int>()),
                    ////////////////////////////////////////
                    j["user_name"].get<std::string>(),
                    j["date"].get<std::string>(),
                    j["state"].get<int>()
            );
        }
    };
}

Thank you :)

Célian Garcia

Edit : NB : I'll use "at" instead of "[]" as mentionned in your doc ;)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions