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

Issue with struct nested in class' to_json #1155

Closed
Qbz23 opened this issue Jul 4, 2018 · 4 comments
Closed

Issue with struct nested in class' to_json #1155

Qbz23 opened this issue Jul 4, 2018 · 4 comments
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@Qbz23
Copy link

Qbz23 commented Jul 4, 2018

I'm getting the "could not find to_json() in T's namespace" error when trying to use a struct nested in a class. I figure I'm most likely just missing something...

The following does not work...

class Tile : public MouseReactive
{
public:

    struct TileData
    {
      std::string name;
      uint32_t redStart;
      uint32_t blueStart;
      uint32_t redGain;
      uint32_t blueGain;
      uint32_t nutrientMax;
      bool impassable;
      std::string tex;
    };
    static void to_json(json& j, const TileData& td);
};

void Tile::to_json(json& j, const TileData& td)
{
  j = json{ {"name",         td.name},
            {"redStart",     td.redStart},
            {"blueStart",    td.blueGain},
            {"redGain",      td.redGain},
            {"blueGain",     td.blueGain},
            {"nutrientMax",  td.nutrientMax},
            {"impassable",   td.impassable},
            {"tex",          td.tex}};
}

But this similar example does. Not sure what I'm missing for classes

namespace TestNs
{
  struct TestType
  {
    std::string name;
    uint32_t redStart;
    uint32_t blueStart;
    uint32_t redGain;
    uint32_t blueGain;
    uint32_t nutrientMax;
    bool impassable;
    std::string tex;
  };
  void to_json(json& j, const TestNs::TestType& tt);
};

namespace TestNs
{
  void to_json(json& j, const TestNs::TestType& tt)
  {
    j = json{ { "name",         tt.name },
              { "redStart",     tt.redStart },
              { "blueStart",    tt.blueGain },
              { "redGain",      tt.redGain },
              { "blueGain",     tt.blueGain },
              { "nutrientMax",  tt.nutrientMax },
              { "impassable",   tt.impassable },
              { "tex",          tt.tex } };
  }
}

And I'm just testing it with

  TestNs::TestType tt;
  json j = tt;
  Tile::TileData td;
  json j2 = td;
@nlohmann
Copy link
Owner

nlohmann commented Jul 4, 2018

You need to define the to_json function in the same namespace as your class. Can you post a complete code example of the issue that raises the error?

@Qbz23
Copy link
Author

Qbz23 commented Jul 4, 2018

Ah I understand. I misinterpreted the readme. I thought I would need to put to_json inside the class because that's where the struct is defined. Just putting the to_json in the global namespace where the class is defined works just fine. Thanks for the quick response!

@Qbz23 Qbz23 closed this as completed Jul 4, 2018
@nlohmann nlohmann added kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation labels Jul 4, 2018
@nlohmann
Copy link
Owner

nlohmann commented Jul 4, 2018

No worries. This aspect of the class leads to a lot of questions, and I am in the course of extending the documentation for this.

@vasishath
Copy link

What a coincidence, I made the exact same mistake in my code.

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