Skip to content

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

Closed
@federicoorta

Description

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

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions