Storm
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
JsonAdapter.cpp
Go to the documentation of this file.
2
3#include <sstream>
4
8
9namespace storm {
10
11template<typename ValueType>
13 STORM_LOG_ASSERT(j.is_number_float(), "Expected a json object of type float.");
14 auto jDump = j.dump();
15 // json::dump() internally converts ValueType to double which might be nan or inf, depending on how exactly the conversion works.
16 // In that case, nlohmann/json will produce "null" (as neither nan nor inf are valid json values).
17 // See https://json.nlohmann.me/api/basic_json/number_float_t/
18 if (jDump == "null") {
19 return false;
20 }
21 // Parse the dumped value with full accuracy
22 auto parsed = storm::utility::convertNumber<storm::RationalNumber, std::string>(jDump);
23 // Check if parsed and actual value coincide.
24 if constexpr (std::is_same_v<ValueType, storm::RationalNumber>) {
25 return parsed == j.template get_ref<ValueType const&>();
26 } else {
27 return parsed == storm::utility::convertNumber<storm::RationalNumber>(j.template get_ref<ValueType const&>());
28 }
29}
30
31template<typename ValueType, typename CallBack>
32void json_for_each_number_float(storm::json<ValueType> const& j, CallBack const& f) {
33 if (j.is_structured()) {
34 for (auto const& ji : j) {
36 }
37 } else if (j.is_number_float()) {
38 f(j);
39 }
40}
41
42template<typename ValueType>
44 std::stringstream message;
45 uint64_t num_bad(0), num_all(0);
46 json_for_each_number_float(j, [&message, &num_bad, &num_all](auto const& v_json) {
47 ++num_all;
48 if (!isJsonNumberExportAccurate(v_json)) {
49 ++num_bad;
50 if (num_bad == 1) {
51 auto const& actualValue = v_json.template get_ref<ValueType const&>();
52 message << "Inaccurate JSON export: The number " << actualValue << " will be exported as " << v_json.dump() << ". ";
53 }
54 };
55 });
56 STORM_LOG_WARN_COND(num_bad == 0, message.str() << "In total, " << num_bad << " of " << num_all << " numbers are inaccurate.");
57}
58
59template<typename ValueType>
60std::string dumpJson(storm::json<ValueType> const& j, bool compact) {
63 }
64 if (compact) {
65 return j.dump();
66 } else {
67 return j.dump(4);
68 }
69}
70
71template std::string dumpJson(storm::json<double> const& j, bool compact = false);
72template std::string dumpJson(storm::json<storm::RationalNumber> const& j, bool compact = false);
73
74} // namespace storm
#define STORM_LOG_ASSERT(cond, message)
Definition macros.h:11
#define STORM_LOG_WARN_COND(cond, message)
Definition macros.h:38
LabParser.cpp.
Definition cli.cpp:18
void warnIfJsonExportNotAccurate(storm::json< ValueType > const &j)
bool isJsonNumberExportAccurate(storm::json< ValueType > const &j)
nlohmann::basic_json< std::map, std::vector, std::string, bool, int64_t, uint64_t, ValueType > json
Definition JsonForward.h:10
std::string dumpJson(storm::json< ValueType > const &j, bool compact)
Dumps the given json object, producing a String.
void json_for_each_number_float(storm::json< ValueType > const &j, CallBack const &f)