OpenEnroth 73e68f7
Loading...
Searching...
No Matches
Macros
Json.h File Reference
#include <string>
#include <nlohmann/json.hpp>
#include "Library/Serialization/Serialization.h"
#include "Utility/Preprocessor.h"
#include "JsonFwd.h"
#include "JsonExceptions.h"

Macros

#define JSON_DISABLE_ENUM_SERIALIZATION   1
 
#define MM_DEFINE_JSON_LEXICAL_SERIALIZATION_FUNCTIONS(TYPE)
 
#define MM_DEFINE_JSON_STRUCT_SERIALIZATION_FUNCTIONS(TYPE, MEMBER_NAMES)
 
#define MM_DEFINE_JSON_STRUCT_MEMBER_SERIALIZATION_I(ARG)   MM_DEFINE_JSON_STRUCT_MEMBER_SERIALIZATION_II ARG
 
#define MM_DEFINE_JSON_STRUCT_MEMBER_SERIALIZATION_II(MEMBER, NAME)    nlohmann::to_json(json[NAME], value.MEMBER);
 
#define MM_DEFINE_JSON_STRUCT_MEMBER_DESERIALIZATION_I(ARG)   MM_DEFINE_JSON_STRUCT_MEMBER_DESERIALIZATION_II ARG
 
#define MM_DEFINE_JSON_STRUCT_MEMBER_DESERIALIZATION_II(MEMBER, NAME)
 

Macro Definition Documentation

◆ JSON_DISABLE_ENUM_SERIALIZATION

#define JSON_DISABLE_ENUM_SERIALIZATION   1

◆ MM_DEFINE_JSON_LEXICAL_SERIALIZATION_FUNCTIONS

#define MM_DEFINE_JSON_LEXICAL_SERIALIZATION_FUNCTIONS (   TYPE)
Value:
void to_json(Json &json, const TYPE &value) { \
nlohmann::to_json(json, toString(value)); \
} \
\
void from_json(const Json &json, TYPE &value) { \
const std::string *jsonString = json.get<const std::string *>(); \
if (!jsonString) \
throwJsonDeserializationError(json, #TYPE); \
value = fromString<TYPE>(*jsonString); \
}
nlohmann::json Json
Definition: JsonFwd.h:5
Json json
Definition: Json_ut.cpp:131
constexpr detail::StringSerializer toString
Definition: Serialization.h:40

Defines enum json serialization functions that just forward into lexical serialization declared via Library/Serialization.

Note that NLOHMANN_JSON_SERIALIZE_ENUM offers no customization of how errors are reported, and this is why we're not using it.

Parameters
TYPEType to define json serialization functions for.

◆ MM_DEFINE_JSON_STRUCT_MEMBER_DESERIALIZATION_I

#define MM_DEFINE_JSON_STRUCT_MEMBER_DESERIALIZATION_I (   ARG)    MM_DEFINE_JSON_STRUCT_MEMBER_DESERIALIZATION_II ARG

◆ MM_DEFINE_JSON_STRUCT_MEMBER_DESERIALIZATION_II

#define MM_DEFINE_JSON_STRUCT_MEMBER_DESERIALIZATION_II (   MEMBER,
  NAME 
)
Value:
if (json.contains(NAME)) \
nlohmann::from_json(json[NAME], value.MEMBER);

◆ MM_DEFINE_JSON_STRUCT_MEMBER_SERIALIZATION_I

#define MM_DEFINE_JSON_STRUCT_MEMBER_SERIALIZATION_I (   ARG)    MM_DEFINE_JSON_STRUCT_MEMBER_SERIALIZATION_II ARG

◆ MM_DEFINE_JSON_STRUCT_MEMBER_SERIALIZATION_II

#define MM_DEFINE_JSON_STRUCT_MEMBER_SERIALIZATION_II (   MEMBER,
  NAME 
)     nlohmann::to_json(json[NAME], value.MEMBER);

◆ MM_DEFINE_JSON_STRUCT_SERIALIZATION_FUNCTIONS

#define MM_DEFINE_JSON_STRUCT_SERIALIZATION_FUNCTIONS (   TYPE,
  MEMBER_NAMES 
)
Value:
void to_json(Json &json, const TYPE &value) { \
} \
\
void from_json(const Json &json, TYPE &value) { \
if (!json.is_object()) \
throwJsonDeserializationError(json, #TYPE); \
}
#define MM_DEFINE_JSON_STRUCT_MEMBER_SERIALIZATION_I(ARG)
Definition: Json.h:56
#define MM_DEFINE_JSON_STRUCT_MEMBER_DESERIALIZATION_I(ARG)
Definition: Json.h:60
#define MM_PP_REMOVE_PARENS(x)
Definition: Preprocessor.h:13

Unfortunately NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT is also very weak when it comes to customization, so we have to roll out our own version.

Parameters
TYPEStruct to define functions for.
MEMBER_NAMESMember-name pairs. Repeats are not supported for now.