Problems serializing struct with nlohmann's json library

huangapple go评论74阅读模式
英文:

Problems serializing struct with nlohmann's json library

问题

I am trying to serialize this struct with nlohmann's json library (the struct is defined in the file JsonResponsePacketSerializer.h):

typedef struct GetRoomsResponse
{
    unsigned int status;
    std::vector<RoomData> rooms;
} GetRoomsResponse;

where RoomData is another struct defined in the file Room.h:

typedef struct RoomData
{
    unsigned int id;
    std::string name;
    unsigned int maxPlayers;
    unsigned int numOfQuestionsInGame;
    unsigned int timePerQuestion;
    unsigned int isActive;
} RoomData;

To serialize this struct, I defined this user-macro as per nlohmann's documentation:

NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(GetRoomsResponse, status, rooms);

But this results in many errors I'm assuming because it doesn't know how to serialize the struct RoomData that is inside the struct GetRoomsResponse.

I tried adding a default constructor to RoomData (even though I'm pretty sure structs have a default constructor by default) and it didn't work.

英文:

I am trying to serialize this struct with nlohmann's json library (the struct is defined in the file JsonResponsePacketSerializer.h):

typedef struct GetRoomsResponse
{
	unsigned int status;
	std::vector&lt;RoomData&gt; rooms;
}GetRoomsResponse;

where RoomData is another struct defined in the file Room.h:

typedef struct RoomData
{
	unsigned int id;
	std::string name;
	unsigned int maxPlayers;
	unsigned int numOfQuestionsInGame;
	unsigned int timePerQuestion;
	unsigned int isActive;
}RoomData;

To serialize this struct, I defined this user-macro as per nlohmann's documentation:

NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(GetRoomsResponse, status, rooms);

But this results in many errors I'm assuming because it doesn't know how to serialize the struct RoomData that is inside the struct GetRoomsResponse.

I tried adding a default constructor to RoomData (even though I'm pretty sure structs have a default constructor by default) and it didn't work.

答案1

得分: 1

以下是您要翻译的内容:

如@AlanBirties在评论中提到的,结构的所有组件必须可序列化,以使结构本身可序列化(文档链接)。

以下是使用标准C++样式声明结构的示例代码。

示例代码

#include <iostream>
#include "nlohmann/json.hpp"

struct RoomData {
    unsigned int id;
    std::string name;
    unsigned int maxPlayers;
    unsigned int numOfQuestionsInGame;
    unsigned int timePerQuestion;
    unsigned int isActive;
};

struct GetRoomsResponse {
    unsigned int status;
    std::vector<RoomData> rooms;
};

NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(RoomData, id, name, maxPlayers, numOfQuestionsInGame, timePerQuestion, isActive);
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(GetRoomsResponse, status, rooms);

int main(int argc, const char *argv[]) {

    RoomData r0{1, "abc", 2, 3, 4, 5}, r1{6, "def", 7, 8, 9, 10};
    GetRoomsResponse resp{0, {r0, r1}};

    nlohmann::json j{resp};
    cout << j << endl;
    return 0;
}

输出

[{"rooms":[{"id":1,"isActive":5,"maxPlayers":2,"name":"abc","numOfQuestionsInGame":3,"timePerQuestion":4},{"id":6,"isActive":10,"maxPlayers":7,"name":"def","numOfQuestionsInGame":8,"timePerQuestion":9}],"status":0}]
英文:

As mentioned in the comments by @AlanBirties, all components of a struct must be serializable for the struct itself to be serializable (documentaiton).

Here is some sample code which also declares the structures in the standard C++ style.

Sample Code

#include &lt;iostream&gt;
#include &quot;nlohmann/json.hpp&quot;

struct RoomData {
    unsigned int id;
    std::string name;
    unsigned int maxPlayers;
    unsigned int numOfQuestionsInGame;
    unsigned int timePerQuestion;
    unsigned int isActive;
};

struct GetRoomsResponse {
    unsigned int status;
    std::vector&lt;RoomData&gt; rooms;
};

NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(RoomData, id, name, maxPlayers, numOfQuestionsInGame, timePerQue\
stion, isActive);
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(GetRoomsResponse, status, rooms);

int main(int argc, const char *argv[]) {

    RoomData r0{1, &quot;abc&quot;, 2, 3, 4, 5}, r1{6, &quot;def&quot;, 7, 8, 9, 10};
    GetRoomsResponse resp{0, {r0, r1}};

    nlohmann::json j{resp};
    cout &lt;&lt; j &lt;&lt; endl;
    return 0;
}

Output

[{&quot;rooms&quot;:[{&quot;id&quot;:1,&quot;isActive&quot;:5,&quot;maxPlayers&quot;:2,&quot;name&quot;:&quot;abc&quot;,&quot;numOfQuestionsInGame&quot;:3,&quot;timePerQue\
stion&quot;:4},{&quot;id&quot;:6,&quot;isActive&quot;:10,&quot;maxPlayers&quot;:7,&quot;name&quot;:&quot;def&quot;,&quot;numOfQuestionsInGame&quot;:8,&quot;timePerQue\
stion&quot;:9}],&quot;status&quot;:0}]

huangapple
  • 本文由 发表于 2023年5月21日 21:23:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76300124.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定