ppforest2 v0.1.0
Projection Pursuit Decision Trees and Random Forests
Loading...
Searching...
No Matches
JsonOptional.hpp
Go to the documentation of this file.
1
14#pragma once
15
16#include <nlohmann/json.hpp>
17#include <optional>
18#include <string_view>
19
29 inline bool has_value(nlohmann::json const& j, std::string_view key) {
30 auto it = j.find(key);
31 return it != j.end() && !it->is_null();
32 }
33}
34
35namespace nlohmann {
36 template<typename T> struct adl_serializer<std::optional<T>> {
37 static void to_json(json& j, std::optional<T> const& opt) {
38 if (opt) {
39 j = *opt;
40 } else {
41 j = nullptr;
42 }
43 }
44
45 static void from_json(json const& j, std::optional<T>& opt) {
46 if (j.is_null()) {
47 opt = std::nullopt;
48 } else {
49 opt = j.get<T>();
50 }
51 }
52 };
53}
nlohmann::json json
Definition CLI.integration.hpp:22
Definition Json.hpp:227
JSON serialization and deserialization for ppforest2 models.
Definition ExportValidation.hpp:5
bool has_value(nlohmann::json const &j, std::string_view key)
True iff j has a non-null value at key.
Definition JsonOptional.hpp:29
static void from_json(json const &j, std::optional< T > &opt)
Definition JsonOptional.hpp:45
static void to_json(json &j, std::optional< T > const &opt)
Definition JsonOptional.hpp:37