config/json.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "terraingen/config/fields/json_io.h" | ||
| 2 | |||
| 3 | #include <cmath> | ||
| 4 | #include <fstream> | ||
| 5 | #include <sstream> | ||
| 6 | #include <string> | ||
| 7 | #include <type_traits> | ||
| 8 | #include <variant> | ||
| 9 | |||
| 10 | namespace terraingen { | ||
| 11 | |||
| 12 | namespace config { | ||
| 13 | |||
| 14 | 70 | void Fail(const std::string& path, const std::string& message) { | |
| 15 | 70 | throw ConfigError(path, message); | |
| 16 | } | ||
| 17 | |||
| 18 | 5001 | Reader::Reader(const Json& json, std::string path) : json_(json), path_(std::move(path)) { | |
| 19 | 5001 | if (!json_.is_object()) { | |
| 20 | 8 | Fail(path_, std::string("expected an object, not ") + json_.type_name()); | |
| 21 | } | ||
| 22 | 5003 | } | |
| 23 | |||
| 24 | 4893 | void Reader::Finish() const { | |
| 25 | 52277 | for (const auto& [key, value] : json_.items()) { | |
| 26 | 23711 | if (known_.count(key) == 0) { | |
| 27 | 19 | std::string expected; | |
| 28 | 201 | for (const std::string& name : known_) { | |
| 29 | 182 | expected += (expected.empty() ? "" : ", ") + name; | |
| 30 | } | ||
| 31 | 57 | Fail(Child(path_, key), "unknown key; expected one of: " + expected); | |
| 32 | 19 | } | |
| 33 | 4912 | } | |
| 34 | 4874 | } | |
| 35 | |||
| 36 | 11111 | void Read(const Json& json, const std::string& path, double& value) { | |
| 37 | 11111 | if (!json.is_number()) { | |
| 38 | 8 | Fail(path, std::string("expected a number, not ") + json.type_name()); | |
| 39 | } | ||
| 40 | 11109 | value = json.get<double>(); | |
| 41 | 11109 | if (!std::isfinite(value)) { | |
| 42 | ✗ | Fail(path, "expected a finite number"); | |
| 43 | } | ||
| 44 | 11109 | } | |
| 45 | |||
| 46 | 373 | void Read(const Json& json, const std::string& path, float& value) { | |
| 47 | 373 | double number = 0.0; | |
| 48 | 373 | Read(json, path, number); | |
| 49 | 373 | if (std::fabs(number) > static_cast<double>(std::numeric_limits<float>::max())) { | |
| 50 | 2 | Fail(path, "number is too large"); | |
| 51 | } | ||
| 52 | 372 | value = static_cast<float>(number); | |
| 53 | 372 | } | |
| 54 | |||
| 55 | 547 | void Read(const Json& json, const std::string& path, bool& value) { | |
| 56 | 547 | if (!json.is_boolean()) { | |
| 57 | 4 | Fail(path, std::string("expected true or false, not ") + json.type_name()); | |
| 58 | } | ||
| 59 | 546 | value = json.get<bool>(); | |
| 60 | 546 | } | |
| 61 | |||
| 62 | 2316 | void Read(const Json& json, const std::string& path, std::string& value) { | |
| 63 | 2316 | if (!json.is_string()) { | |
| 64 | 4 | Fail(path, std::string("expected a string, not ") + json.type_name()); | |
| 65 | } | ||
| 66 | 2315 | value = json.get<std::string>(); | |
| 67 | 2315 | } | |
| 68 | |||
| 69 | 1765 | void Read(const Json& json, const std::string& path, int& value) { | |
| 70 | 1765 | if (!json.is_number_integer()) { | |
| 71 | 8 | Fail(path, std::string("expected an integer, not ") + json.type_name()); | |
| 72 | } | ||
| 73 | 3525 | if (json.is_number_unsigned() && | |
| 74 | 1762 | json.get<std::uint64_t>() > static_cast<std::uint64_t>(std::numeric_limits<int>::max())) { | |
| 75 | 2 | Fail(path, "integer is too large"); | |
| 76 | } | ||
| 77 | 1762 | const auto number = json.get<std::int64_t>(); | |
| 78 | 1762 | if (number < std::numeric_limits<int>::min() || number > std::numeric_limits<int>::max()) { | |
| 79 | 2 | Fail(path, "integer is out of range"); | |
| 80 | } | ||
| 81 | 1761 | value = static_cast<int>(number); | |
| 82 | 1761 | } | |
| 83 | |||
| 84 | namespace { | ||
| 85 | |||
| 86 | /// Sets `output.format` from its path's extension (.json or else CSV) when `json` gives none. | ||
| 87 | 79 | void InferListFormat(const Json& json, ListOutput& output) { | |
| 88 | 79 | if (!json.contains("format")) { | |
| 89 | 65 | const std::string& path = output.path; | |
| 90 | 65 | const bool isJson = path.size() >= 5 && path.compare(path.size() - 5, 5, ".json") == 0; | |
| 91 | 65 | output.format = isJson ? ListFormat::Json : ListFormat::Csv; | |
| 92 | } | ||
| 93 | 79 | } | |
| 94 | |||
| 95 | /// Checks a configuration's "schema_version" before any other key, which a newer version may add. | ||
| 96 | /// @throws ConfigError at "schema_version" when the object `json` has none, or another version | ||
| 97 | /// than this library reads. | ||
| 98 | 359 | void CheckSchemaVersion(const Json& json) { | |
| 99 | 359 | if (!json.is_object()) { | |
| 100 | 2 | return; | |
| 101 | } | ||
| 102 | const std::string supported = | ||
| 103 | 357 | "this library reads version " + std::to_string(kConfigSchemaVersion); | |
| 104 | 357 | const auto found = json.find("schema_version"); | |
| 105 | 357 | if (found == json.end()) { | |
| 106 | 8 | Fail("schema_version", "required; " + supported); | |
| 107 | } | ||
| 108 | 355 | int version = 0; | |
| 109 | 358 | Read(*found, "schema_version", version); | |
| 110 | 352 | if (version != kConfigSchemaVersion) { | |
| 111 | 8 | Fail("schema_version", | |
| 112 | 24 | "version " + std::to_string(version) + " is not supported; " + supported); | |
| 113 | } | ||
| 114 | 357 | } | |
| 115 | |||
| 116 | } // namespace | ||
| 117 | |||
| 118 | 57 | void AfterRead(const Json& json, CraterListOutput& output) { | |
| 119 | 57 | InferListFormat(json, output); | |
| 120 | 57 | } | |
| 121 | |||
| 122 | 22 | void AfterRead(const Json& json, BoulderListOutput& output) { | |
| 123 | 22 | InferListFormat(json, output); | |
| 124 | 22 | } | |
| 125 | |||
| 126 | } // namespace config | ||
| 127 | |||
| 128 | 250 | ConfigError::ConfigError(std::string path, const std::string& message) | |
| 129 | 250 | : std::runtime_error(path.empty() ? message : path + ": " + message), path_(std::move(path)) {} | |
| 130 | |||
| 131 | 361 | TerrainConfig ParseConfig(std::string_view json) { | |
| 132 | 361 | config::Json document; | |
| 133 | try { | ||
| 134 | 363 | document = config::Json::parse(json); | |
| 135 | 2 | } catch (const config::Json::exception& error) { | |
| 136 | 10 | throw ConfigError("", std::string("invalid JSON: ") + error.what()); | |
| 137 | 2 | } | |
| 138 | 359 | config::CheckSchemaVersion(document); | |
| 139 | 350 | TerrainConfig parsed; | |
| 140 | 411 | config::Read(document, "", parsed); | |
| 141 | 578 | return parsed; | |
| 142 | 422 | } | |
| 143 | |||
| 144 | 94 | TerrainConfig LoadConfig(const std::filesystem::path& path) { | |
| 145 | 94 | std::ifstream file(path, std::ios::binary); | |
| 146 | 94 | if (!file) { | |
| 147 | 9 | throw ConfigError("", "cannot read " + path.string()); | |
| 148 | } | ||
| 149 | 91 | std::ostringstream text; | |
| 150 | 91 | text << file.rdbuf(); | |
| 151 | 182 | return ParseConfig(text.str()); | |
| 152 | 95 | } | |
| 153 | |||
| 154 | 66 | std::string ConfigToJson(const TerrainConfig& terrainConfig, int indent) { | |
| 155 | 66 | config::Json json = config::ToJson(terrainConfig); | |
| 156 | 66 | if (!terrainConfig.base) { | |
| 157 | 59 | json.erase("base"); | |
| 158 | } | ||
| 159 | 66 | if (!terrainConfig.materials) { | |
| 160 | 63 | json.erase("materials"); | |
| 161 | } | ||
| 162 | 66 | if (!terrainConfig.albedo) { | |
| 163 | 61 | json.erase("albedo"); | |
| 164 | } | ||
| 165 | 132 | return json.dump(indent) + "\n"; | |
| 166 | 66 | } | |
| 167 | |||
| 168 | 321 | std::string LayerType(const LayerSpec& layer) { | |
| 169 | return std::visit( | ||
| 170 | 321 | [](const auto& spec) { | |
| 171 | using Spec = std::decay_t<decltype(spec)>; | ||
| 172 | 321 | std::string type(config::SpecName<Spec>::kName); | |
| 173 | if constexpr (std::is_same_v<Spec, RegionLayerSpec>) { | ||
| 174 | 7 | type += " (" + LayerType(*spec.layer) + ")"; | |
| 175 | } | ||
| 176 | 321 | return type; | |
| 177 | ✗ | }, | |
| 178 | 642 | layer); | |
| 179 | } | ||
| 180 | |||
| 181 | } // namespace terraingen | ||
| 182 |