ArcdpsExtension
 
Loading...
Searching...
No Matches
nlohmannJsonExtension.h
Go to the documentation of this file.
1#pragma once
2#include <optional>
3
4#include <nlohmann/json.hpp>
5
6#define NLOHMANN_JSON_FROM_NON_THROWING(v1) \
7 if (nlohmann_json_j.contains(#v1)) nlohmann_json_j.at(#v1).get_to(nlohmann_json_t.v1);
8#define NLOHMANN_DEFINE_TYPE_INTRUSIVE_NON_THROWING(Type, ...) \
9 friend void to_json(nlohmann::json& nlohmann_json_j, const Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } \
10 friend void from_json(const nlohmann::json& nlohmann_json_j, Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM_NON_THROWING, __VA_ARGS__)) }
11
12#define NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_NON_THROWING(Type, ...) \
13 inline void to_json(nlohmann::json& nlohmann_json_j, const Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } \
14 inline void from_json(const nlohmann::json& nlohmann_json_j, Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM_NON_THROWING, __VA_ARGS__)) }
15
16
17// add support for std::optional (3.11.3), previously used code removed
18template<typename T>
19struct nlohmann::adl_serializer<std::optional<T>> {
20 static void to_json(nlohmann::json& j, const std::optional<T>& v) {
21 if (v.has_value()) {
22 j = v.value();
23 } else {
24 j = nullptr;
25 }
26 }
27 static void from_json(const nlohmann::json& j, std::optional<T>& v) {
28 if(j.is_null()) {
29 v = std::nullopt;
30 } else {
31 v = j.get<T>();
32 }
33 }
34};
static void from_json(const nlohmann::json &j, std::optional< T > &v)
Definition nlohmannJsonExtension.h:27
static void to_json(nlohmann::json &j, const std::optional< T > &v)
Definition nlohmannJsonExtension.h:20