include/terraingen/config/json.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | /// @file | ||
| 4 | /// @brief Configurations as JSON: parsing, loading, writing, catalog and schema. | ||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include "terraingen/config/config.h" | ||
| 8 | |||
| 9 | #include <filesystem> | ||
| 10 | #include <stdexcept> | ||
| 11 | #include <string> | ||
| 12 | #include <string_view> | ||
| 13 | |||
| 14 | namespace terraingen { | ||
| 15 | |||
| 16 | /// A problem with a configuration, located by the JSON path of the offending value, such as | ||
| 17 | /// "layers[1].density.age_gyr" or "layers[0].stack[1].octaves". | ||
| 18 | class ConfigError : public std::runtime_error { | ||
| 19 | public: | ||
| 20 | /// An error at a path; the message is "path: message", or the message alone at the root. | ||
| 21 | /// @param path The JSON path of the value; empty for the document as a whole. | ||
| 22 | ConfigError(std::string path, const std::string& message); | ||
| 23 | |||
| 24 | /// The JSON path of the value; empty for the document as a whole. | ||
| 25 | 393 | const std::string& Path() const { | |
| 26 | 393 | return m_path; | |
| 27 | } | ||
| 28 | |||
| 29 | private: | ||
| 30 | std::string m_path; | ||
| 31 | }; | ||
| 32 | |||
| 33 | /// Reads a configuration from JSON text. | ||
| 34 | /// | ||
| 35 | /// {"schema_version": 1, | ||
| 36 | /// "layers": [{"type": "noise", "stack": [{"wavelength_m": 500, "amplitude_m": 50}]}, | ||
| 37 | /// {"type": "craters", | ||
| 38 | /// "density": {"type": "production_surface", "age_gyr": 3.5}, | ||
| 39 | /// "apply": {"rim_smoothing": {"sigma_radii": 0.05}}}], | ||
| 40 | /// "outputs": [{"type": "heightmap", "path": "terrain.tif", | ||
| 41 | /// "pixel_size_m": 2, | ||
| 42 | /// "extent_m": {"center": [0, 0], "size": [1026, 1026]}}]} | ||
| 43 | /// | ||
| 44 | /// Keys are snake_case versions of the C++ fields; bodies, sources, layers, outputs and models are | ||
| 45 | /// objects with a "type". Omitted keys take their defaults and omitted lists are empty. | ||
| 46 | /// @throws ConfigError for malformed JSON, a missing or unsupported "schema_version", an unknown | ||
| 47 | /// key, or a value of the wrong type. | ||
| 48 | TerrainGenConfig ParseConfig(std::string_view json); | ||
| 49 | |||
| 50 | /// `ParseConfig` on the contents of a file, remembering its folder in `directory`. | ||
| 51 | /// @throws ConfigError as `ParseConfig`, or when the file cannot be read. | ||
| 52 | TerrainGenConfig LoadConfig(const std::filesystem::path& path); | ||
| 53 | |||
| 54 | /// A configuration as JSON with every value written out but an unset source and albedo; parsing it | ||
| 55 | /// gives back an equal configuration. | ||
| 56 | std::string ConfigToJson(const TerrainGenConfig& config, int indent = 2); | ||
| 57 | |||
| 58 | /// Every configuration type with its defaults, as JSON: lists "body", "source", "layers", | ||
| 59 | /// "outputs" and "models" whose entries each parse where their kind belongs, and the "albedo". | ||
| 60 | std::string ConfigCatalog(int indent = 2); | ||
| 61 | |||
| 62 | /// The structure of a configuration as JSON, for editors, from the field lists the parser reads: | ||
| 63 | /// "root" and "definitions" of schemas, each with a "kind" (number, integer, boolean, string, enum, | ||
| 64 | /// optional, array, object, choice or ref) and, for fields, their description, unit, default and | ||
| 65 | /// bounds. | ||
| 66 | std::string ConfigSchema(int indent = 2); | ||
| 67 | |||
| 68 | } // namespace terraingen | ||
| 69 |