Skip to content

How do i include a vector of pointers in my json? #1997

@Leonetienne

Description

@Leonetienne

I need a result like this:

{
  "result": "success",
  "num_messages": 45,
  "messages":
  [
    {
      "message": "beep boop",
      "timestamp": 39348
    },
    {
      "message": "blib blub",
      "timestamp": 69420
    },
//...
  ]
}

I have the class LogHistory::LogEntry (class inside of another class) and LogHistory provides a static getter for an std::vector<LogEntry*>*

All fine and good for normal iterating, but how do i put this thing into a json?
I tried the to_json(json& j, LogEntry& le) conversion method, but i just get "no suitable conversion method found"

Both of these two get underlined red with "no suitable conversion method found"

json j = LogHistory::GetLogHistory()->at(3);
json j = *LogHistory::GetLogHistory()->at(3);

I think i may have placed the conversion method in the wrong namespace? Right now it's a static member of LogEntry...

Here is my class structure again for clarity...

class LogHistory
{
   public:
      class LogEntry
      {
         public:
            std::string message;
            int timestamp;

            static void to_json(Json& j, const LogEntry& le)
            {
                j = {{"message", le.message}, {"timestamp", le.timestamp}};
            }
      };

      static std::vector<LogEntry*>* GetLogHistory() { return logHistory; }

      private:
         static std::vector<LogEntry*>* logHistory;
};

Would be very happy if someone could point me in the right direction

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions