Skip to content

Issue with struct nested in class' to_json #1155

Description

@Qbz23

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;

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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions