In the Engine\Source\Runtime\Online\OnlineSubsystem\Public\OnlineJsonSerializer.h file there are multiple #define statements. This one is not working as expected when trying to read the object:
#define ONLINE_JSON_SERIALIZE_SERIALIZABLE(JsonName, JsonValue) \
JsonValue.Serialize(Serializer)
So I have taken the liberty of modifying it (and renaming it) to the following that works for both reading and writing these serializable JSON Objects:
#define ONLINE_JSON_SERIALIZE_OBJECT(JsonName, JsonSerializableObject) \
/* Process the JsonName field differently because it is an object, no other macro seems to work */ \
if (Serializer.IsLoading()) \
{ \
/* Read in the value from the JsonName field */ \
if (Serializer.GetObject()->HasTypedField<EJson::Object>(JsonName)) \
{ \
TSharedPtr<FJsonObject> JsonObj = Serializer.GetObject()->GetObjectField(JsonName); \
if (JsonObj.IsValid()) \
{ \
JsonSerializableObject.FromJson(JsonObj); \
} \
} \
} \
else \
{ \
/* Write the value to the JsonName field */ \
Serializer.StartObject(JsonName); \
JsonSerializableObject.Serialize(Serializer); \
Serializer.EndObject(); \
}
Would it be possible to get this included into the base engine source code as I think it is a very handy feature to have.
For instance I have two FOnlineJsonSerializable objects and one is contained within the other. This now allows me to do simple things like the following:
struct FJsonServerSettings : public FOnlineJsonSerializable
{
...
BEGIN_ONLINE_JSON_SERIALIZER
ONLINE_JSON_SERIALIZE("name", ServerName);
ONLINE_JSON_SERIALIZE("ver", ServerVersion);
ONLINE_JSON_SERIALIZE("motd", ServerMessageOfTheDay);
ONLINE_JSON_SERIALIZE("players", PlayersOnline);
END_ONLINE_JSON_SERIALIZER
};
class FJsonInternetSession : public FOnlineJsonSerializable
{
...
/** Settings for this server's registration */
FJsonServerSettings ServerSettings;
// FJsonSerializable
BEGIN_ONLINE_JSON_SERIALIZER
ONLINE_JSON_SERIALIZE("ip", ServerIP);
ONLINE_JSON_SERIALIZE("port", ServerPort);
ONLINE_JSON_SERIALIZE("name", ServerName);
ONLINE_JSON_SERIALIZE_OBJECT("settings", ServerSettings);
END_ONLINE_JSON_SERIALIZER
};
Note that the ServerSettings object can now be read and written to from within this class as if it were a member of this JsonObject.