Skip to content

templated from_json of non primitive types causes gcc error #2168

Description

@kouberl

What is the issue you have?

I use templated to_json/from_json methods, because I have different serializer for the same type.
I have problems with gcc with the from_json methods.

Please describe the steps to reproduce the issue.

When I use the json::get method it does not compile.

Can you provide a small but working code example?

#include <nlohmann/json.hpp>
#include <vector>

namespace ns {
    struct MyType {
        std::vector<int> i;
    };

    template <typename json_t>
    void to_json(json_t &j, const MyType &m) {
        j["i"] = m.i;
    }

    template <typename json_t> 
    void from_json(json_t const& j, MyType& m) {
        m.i = j.at("i").get<std::vector<int>>();
    }
}

int main() {
    ns::MyType type;
    type.i.push_back(42);
    nlohmann::json type_json = type;
    ns::MyType type2 = type_json;
}

As a workaround I inserted another copy: (toogle define to enable the workaround)
see https://godbolt.org/z/uVW_4b

What is the expected behavior?

should compile

And what is the actual behavior instead?

gcc error

Which compiler and operating system are you using?

  • Compiler: gcc
  • Operating system: linux

Which version of the library did you use?

  • [x ] latest release version 3.7.3
  • other release - please state the version: ___
  • the develop branch

If you experience a compilation error: can you compile and run the unit tests?

  • [ x] yes
  • no - please copy/paste the error message below

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

    Labels

    kind: bugsolution: proposed fixa fix for the issue has been proposed and waits for confirmation

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions