MQTTSuite
Loading...
Searching...
No Matches
JsonMappingReader.cpp
Go to the documentation of this file.
1/*
2 * MQTTSuite - A lightweight MQTT Integration System
3 * Copyright (C) Volker Christian <me@vchrist.at>
4 * 2022, 2023, 2024, 2025
5 *
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation, either version 3 of the License, or (at your option)
9 * any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20/*
21 * MIT License
22 *
23 * Permission is hereby granted, free of charge, to any person obtaining a copy
24 * of this software and associated documentation files (the "Software"), to deal
25 * in the Software without restriction, including without limitation the rights
26 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
27 * copies of the Software, and to permit persons to whom the Software is
28 * furnished to do so, subject to the following conditions:
29 *
30 * The above copyright notice and this permission notice shall be included in
31 * all copies or substantial portions of the Software.
32 *
33 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
39 * THE SOFTWARE.
40 */
41
43
44#include "nlohmann/json-schema.hpp"
45
46#ifndef DOXYGEN_SHOULD_SKIP_THIS
47
48#include <exception>
49#include <fstream>
50#include <log/Logger.h>
51#include <nlohmann/json.hpp>
52
53#endif
54
55namespace mqtt::lib {
56
57#include "mapping-schema.json.h" // definition of mappingJsonSchemaString
58
60
62
63 nlohmann::json& JsonMappingReader::readMappingFromFile(const std::string& mapFilePath) {
64 if (!mapFileJsons.contains(mapFilePath)) {
65 if (!mapFilePath.empty()) {
66 std::ifstream mapFile(mapFilePath);
67
68 if (mapFile.is_open()) {
69 VLOG(1) << "MappingFilePath: " << mapFilePath;
70
71 try {
72 mapFile >> mapFileJsons[mapFilePath];
73
74 try {
76
77 try {
78 const nlohmann::json defaultPatch = validator.validate(mapFileJsons[mapFilePath]);
79
80 try {
81 mapFileJsons[mapFilePath] = mapFileJsons[mapFilePath].patch(defaultPatch);
82 } catch (const std::exception& e) {
83 VLOG(1) << e.what();
84 VLOG(1) << "Patching JSON with default patch failed:\n" << defaultPatch.dump(4);
85 mapFileJsons[mapFilePath].clear();
86 }
87 } catch (const std::exception& e) {
88 VLOG(1) << " Validating JSON failed:\n" << mapFileJsons[mapFilePath].dump(4);
89 VLOG(1) << " " << e.what();
90 mapFileJsons[mapFilePath].clear();
91 }
92 } catch (const std::exception& e) {
93 VLOG(1) << e.what();
94 VLOG(1) << "Setting root json mapping schema failed:\n" << mappingJsonSchema.dump(4);
95 mapFileJsons[mapFilePath].clear();
96 }
97 } catch (const std::exception& e) {
98 VLOG(1) << "JSON map file parsing failed: " << e.what() << " at " << mapFile.tellg();
99 mapFileJsons[mapFilePath].clear();
100 }
101
102 mapFile.close();
103 } else {
104 VLOG(1) << "MappingFilePath: " << mapFilePath << " not found";
105 }
106 } else {
107 VLOG(1) << "MappingFilePath empty";
108 }
109 }
110
111 return mapFileJsons[mapFilePath];
112 }
113
114} // namespace mqtt::lib
static nlohmann::json mappingJsonSchema
static nlohmann::json & readMappingFromFile(const std::string &mapFilePath)
static const std::string mappingJsonSchemaString
static std::map< std::string, nlohmann::json > mapFileJsons
json_validator(const json &, schema_loader=nullptr, format_checker=nullptr, content_checker=nullptr)