3

I have Class RoomData with this fields:

#include <string>

class RoomData
{
public:
    int id;
    string name;
    int maxPlayers;
    int timePerQuestion;
    int isActive;
};

and i'm trying to turn vactor of RoomData Vector<RoomData> to json array this what i tried:

#include <nlohmann/json.hpp>

using nlohmann::json;

string serialize(vector<RoomData> roomData)
{
    json j(roomData);
    string jsonArray = j.dump();
    return jsonArray;
}

But it gives me these Errors: C2338 forcing MSVC stacktrace to show which T we're talking about.
C2338 could not find to_json() method in T's namespace
C2065 'force_msvc_stacktrace': undeclared identifier
C2825 'decayed': must be a class or namespace when followed by '::'
C2510 'decayed': left of '::' must be a class/struct/union

Osher
  • 445
  • 5
  • 12
  • Also, what library your class `json` is from? – HolyBlackCat May 08 '18 at 15:21
  • i'm using in json.hpp @HolyBlackCat – Osher May 08 '18 at 15:28
  • It works for struct not class – BartekPL May 08 '18 at 15:38
  • 1
    "json.hpp" is not enough information. I could make a file called "json.hpp" right now, with any old contents. Which library are you using? What is it called, who wrote it, what version is it? Provide _detailed_ information. – Lightness Races in Orbit May 08 '18 at 15:45
  • @BartekPL: _"It works for struct not class"_ C++ only has classes so that makes no sense. – Lightness Races in Orbit May 08 '18 at 15:45
  • @LightnessRacesinOrbit I'm sure that struct is part od C++ as well as classes. – BartekPL May 08 '18 at 15:48
  • @BartekPL structs are syntactic sugar over classes. Or the other way around. It just changes the defaults and nothing more. – Dan M. May 08 '18 at 15:49
  • @NoneName with the limited info you've provided my only guess is that your json lib doesn't know how to serialize your class. And how would it know (there is no reflection in C++)? Judging by the error, you need to define to_json() function for your RoomData. Consult your lib documentation to figure out how to do this, it probably has examples. – Dan M. May 08 '18 at 15:52
  • @BartekPL: Regardless of your sureness, you are mistaken. The keyword `struct` and the keyword `class` both define a _class_. This [has been](https://stackoverflow.com/a/35258401/560648) [well-covered](https://stackoverflow.com/a/36917400/560648) [elsewhere](https://stackoverflow.com/a/34108140/560648). – Lightness Races in Orbit May 08 '18 at 16:13
  • im using in this libary: https://github.com/nlohmann/json/blob/develop/include/nlohmann/json.hpp @LightnessRacesinOrbit – Osher May 08 '18 at 16:55
  • Put such detail in the question please, along with your [MCVE]. – Lightness Races in Orbit May 08 '18 at 17:04

1 Answers1

4

I'm guessing that you're using https://github.com/nlohmann/json.

To make this work with one of your types, you only need to provide two functions:

using nlohmann::json;

void to_json(json& j, const RoomData& r) {
    j = json{
        {"id", r.id}, 
        {"name", r.name}, 
        {"maxPlayers", r.maxPlayers}, 
        {"timePerQuestion", r.timePerQuestion}, 
        {"isActive", r.isActive}
    };
}

void from_json(const json& j, RoomData& r) {
    r.id = j.at("id").get<int>();
    r.name = j.at("name").get<std::string>();
    r.maxPlayers = j.at("maxPlayers").get<int>();
    r.timePerQuestion = j.at("timePerQuestion").get<int>();
    r.isActive = j.at("timePerQuestion").get<int>();
}

Then this will work. Hope this helps up. Cheers.

Paul Varghese
  • 1,635
  • 1
  • 15
  • 30
  • 1
    @PaulVarghese: What are you talking about? Your answer is only 15 minutes old, and the OP commented 10 minutes ago... – Lightness Races in Orbit May 08 '18 at 17:08
  • With this library it's possible to convert from STL containers Check https://github.com/nlohmann/json#conversion-from-stl-containers – Paul Varghese May 08 '18 at 17:10
  • @PaulVarghese But there not example of Vector.. only of simple types like int, double, string.. i want to know how it look if i have in class more types (Vector) – Osher May 08 '18 at 17:14
  • @OsherAsefa Please find the complete example in this link. Check the last part to see how it works. https://wandbox.org/permlink/mBR5irlVECxzNpOr – Paul Varghese May 08 '18 at 19:30