DNDSR 0.1.0.dev1+gcd065ad
Distributed Numeric Data Structure for CFV
Loading...
Searching...
No Matches
JsonUtil.hpp File Reference

JSON-to-Eigen conversion utilities and nlohmann_json helper macros. More...

#include "Defines.hpp"
#include <nlohmann/json.hpp>
#include "EigenUtil.hpp"
#include "DeviceStorage.hpp"
#include "Vector.hpp"
Include dependency graph for JsonUtil.hpp:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Namespaces

namespace  DNDS
 the host side operators are provided as implemented
 
namespace  Eigen
 

Macros

#define JSON_ASSERT   DNDS_assert
 
#define __DNDS__json_to_config(name)
 Helper macro: read (read == true) or write (read == false) a named member into / out of a jsonObj.
 
#define DNDS_NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_ORDERED_JSON(Type, ...)
 Like NLOHMANN_DEFINE_TYPE_INTRUSIVE but targets nlohmann::ordered_json.
 
#define DNDS_NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_ORDERED_AND_UNORDERED_JSON(Type, ...)
 Like DNDS_NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_ORDERED_JSON but additionally installs the unordered-JSON overloads for interop with code that uses nlohmann::json directly.
 

Typedefs

using DNDS::t_jsonconfig = nlohmann::ordered_json
 Project-wide JSON type alias: nlohmann/json with ordered keys.
 

Functions

Eigen::VectorXd DNDS::JsonGetEigenVector (const nlohmann::json &arr)
 Parse a JSON array into an Eigen::VectorXd. Throws a descriptive assertion on any JSON error.
 
Eigen::VectorFMTSafe< real, -1 > DNDS::JsonGetEigenVectorFMTSafe (const nlohmann::json &arr)
 Parse a JSON array into an Eigen::VectorFMTSafe (fixed-point-aware wrapper).
 
auto DNDS::EigenVectorGetJson (const Eigen::VectorXd &ve)
 Dump an Eigen::VectorXd into a JSON array of doubles.
 
auto DNDS::EigenVectorFMTSafeGetJson (const Eigen::VectorFMTSafe< real, -1 > &ve)
 Dump an Eigen::VectorFMTSafe into a JSON array of doubles.
 
void Eigen::to_json (nlohmann::json &j, const VectorXd &v)
 
void Eigen::from_json (const nlohmann::json &j, VectorXd &v)
 
void Eigen::to_json (nlohmann::ordered_json &j, const VectorXd &v)
 
void Eigen::from_json (const nlohmann::ordered_json &j, VectorXd &v)
 
void Eigen::to_json (nlohmann::ordered_json &j, const VectorFMTSafe< DNDS::real, -1 > &v)
 
void Eigen::from_json (const nlohmann::ordered_json &j, VectorFMTSafe< DNDS::real, -1 > &v)
 
void Eigen::to_json (nlohmann::ordered_json &j, const Vector3d &v)
 
void Eigen::from_json (const nlohmann::ordered_json &j, Vector3d &v)
 
void DNDS::to_json (nlohmann::ordered_json &j, const host_device_vector< real > &v)
 
void DNDS::from_json (const nlohmann::ordered_json &j, host_device_vector< real > &v)
 

Detailed Description

JSON-to-Eigen conversion utilities and nlohmann_json helper macros.

Definition in file JsonUtil.hpp.

Macro Definition Documentation

◆ __DNDS__json_to_config

#define __DNDS__json_to_config (   name)
Value:
{ \
if (read) \
try \
{ \
((name) = jsonObj.at(#name).template get<decltype(name)>()); \
} \
catch (const std::exception &v) \
{ \
std::cerr << v.what() << std::endl; \
DNDS_assert_info(false, #name); \
} \
else \
(jsonObj[#name] = (name)); \
}
Eigen::Matrix< real, 5, 1 > v

Helper macro: read (read == true) or write (read == false) a named member into / out of a jsonObj.

Used in the common pattern of mirroring a config struct between JSON and C++:

#define __F(v) __DNDS__json_to_config(v)
__F(gamma); __F(CFL); __F(maxIter);

Errors during reading are surfaced via DNDS_assert_info with the member name.

Definition at line 98 of file JsonUtil.hpp.

◆ DNDS_NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_ORDERED_AND_UNORDERED_JSON

#define DNDS_NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_ORDERED_AND_UNORDERED_JSON (   Type,
  ... 
)
Value:
friend void to_json(nlohmann::ordered_json &nlohmann_json_j, const Type &nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } \
friend void from_json(const nlohmann::ordered_json &nlohmann_json_j, Type &nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) } \
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__)) } \
friend void from_json(const nlohmann::json &nlohmann_json_j, Type &nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) }
void from_json(const nlohmann::json &j, VectorXd &v)
Definition JsonUtil.hpp:139
void to_json(nlohmann::json &j, const VectorXd &v)
Definition JsonUtil.hpp:134

Like DNDS_NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_ORDERED_JSON but additionally installs the unordered-JSON overloads for interop with code that uses nlohmann::json directly.

Definition at line 125 of file JsonUtil.hpp.

◆ DNDS_NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_ORDERED_JSON

#define DNDS_NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_ORDERED_JSON (   Type,
  ... 
)
Value:
friend void to_json(nlohmann::ordered_json &nlohmann_json_j, const Type &nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } \
friend void from_json(const nlohmann::ordered_json &nlohmann_json_j, Type &nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) }

Like NLOHMANN_DEFINE_TYPE_INTRUSIVE but targets nlohmann::ordered_json.

DNDSR prefers ordered JSON for configuration; this macro wires up both to_json and from_json friend functions against the ordered type.

Definition at line 118 of file JsonUtil.hpp.

◆ JSON_ASSERT

#define JSON_ASSERT   DNDS_assert

Definition at line 6 of file JsonUtil.hpp.