Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to locate 'to_json()' and 'from_json()' methods in the same namespace #917

Closed
federicoorta opened this issue Jan 16, 2018 · 3 comments
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@federicoorta
Copy link

federicoorta commented Jan 16, 2018

I am trying out this nice library and to learn how to use it, after running the examples, I started working on a very small example on my own.

Here it is:

namespace defe {
    class Item {

    public:
        { constructor/destructor }

        { getters&setters }

        void to_json(json& j, const defe::Item& item) {
            j = json{{"name",                item.name},
                     {"type",                item.type},
                     {"lengthInByte",        item.lengthInByte},
                     {"elementDescriptions", item.elementDescriptions}};
        }

        void from_json(const json& j, defe::Item& item) {
            item.name = j.at("name").get<std::string>();
            item.type = j.at("type").get<std::string>();
            item.lengthInByte = j.at("lengthInByte").get<unsigned short int>();
            item.elementDescriptions = j.at("elementDescriptions").get<std::vector<std::vector<std::string>>>();
        }

    private:
        std::string name;
        std::string type;
        unsigned short int lengthInByte;
        std::vector<std::vector<std::string>> elementDescriptions;
    };

    class CategoryDescription {

    public:
        { constructor/destructor }

        { getters&setters }

        void to_json(json& j, const defe::CategoryDescription& catDesc) {
            j = json{{"item", catDesc.item}};
        }

        void from_json(const json& j, defe::CategoryDescription& catDesc) {
            catDesc.item = j.at("itemCollection").get<std::vector<defe::Item>>();
        }

    private:
        std::vector<defe::Item> items;
    };
}

I compiled it twice: the first one, just with the Item class and it has been successful.
Then, I added the CategoryDescription class, where I have a vector of Item instances.
GCC 7.2.0 on Ubuntu 17.10

When I try to compile the complete version (you find here), the compilation fails with the following error:

error: static assertion failed: could not find from_json() method in T's namespace
         static_assert(sizeof(BasicJsonType) == 0

I learned from the docs and from other issue answers that such an error tells that the to_json() and from_json() methods have not been located, isn't it?
So my question is: am I required to write a specialization of adl_serializer, even if both classes belongs to the same namespace?

I am a bit confused, I apologize for that. It might be that I am mixing up two different problems.
But I am sure to have defined the two classes in the right order (ref. issue 561). The latter class (CategoryDescription) is making use of the former class (Item).

Thank you for your help :)
If I have missed something, feel free to ask further details.

Best Regards

@federicoorta federicoorta changed the title Unable to locate 'to_json()' and 'from_json()' methods Unable to locate 'to_json()' and 'from_json()' methods in the same namespace Jan 16, 2018
@theodelrieu
Copy link
Contributor

No worries, you don't have to write a serializer, your issue is that the to_json/from_json functions MUST BE free functions (i.e. outside of your class).

Moving them outside should fix your problem.

@federicoorta
Copy link
Author

Oh.. just that? :)
I don't know why I thought that those methods should have been member functions..

I will try again later today and I will come back to report the result.
Thank you!

@nlohmann nlohmann added kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation labels Jan 16, 2018
@federicoorta
Copy link
Author

Thank you, Théo! All is working! 👍

Now it's time to go further with this great library! Thank you, Niels too! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Projects
None yet
Development

No branches or pull requests

3 participants