MQTTSuite
Loading...
Searching...
No Matches
mqtt::lib::JsonMappingReader Class Reference

#include <JsonMappingReader.h>

Collaboration diagram for mqtt::lib::JsonMappingReader:

Public Member Functions

 JsonMappingReader ()=delete

Static Public Member Functions

static nlohmann::json & readMappingFromFile (const std::string &mapFilePath)

Static Private Attributes

static nlohmann::json mappingJsonSchema = nlohmann::json::parse(mappingJsonSchemaString)
static const std::string mappingJsonSchemaString
static std::map< std::string, nlohmann::json > mapFileJsons

Detailed Description

Definition at line 55 of file JsonMappingReader.h.

Constructor & Destructor Documentation

◆ JsonMappingReader()

mqtt::lib::JsonMappingReader::JsonMappingReader ( )
delete

Member Function Documentation

◆ readMappingFromFile()

nlohmann::json & mqtt::lib::JsonMappingReader::readMappingFromFile ( const std::string & mapFilePath)
static

Definition at line 63 of file JsonMappingReader.cpp.

63 {
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 {
75 const nlohmann::json_schema::json_validator validator(mappingJsonSchema);
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 }
nlohmann::json_schema::json_validator validator
static nlohmann::json mappingJsonSchema
static std::map< std::string, nlohmann::json > mapFileJsons

References nlohmann::json_schema::json_validator::json_validator(), mapFileJsons, and mappingJsonSchema.

Referenced by mqtt::mqttbroker::SocketContextFactory::create(), mqtt::mqttintegrator::SocketContextFactory::create(), and mqtt::mqttintegrator::websocket::SubProtocolFactory::create().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ mapFileJsons

std::map< std::string, nlohmann::json > mqtt::lib::JsonMappingReader::mapFileJsons
staticprivate

Definition at line 65 of file JsonMappingReader.h.

Referenced by readMappingFromFile().

◆ mappingJsonSchema

nlohmann::json mqtt::lib::JsonMappingReader::mappingJsonSchema = nlohmann::json::parse(mappingJsonSchemaString)
staticprivate

Definition at line 62 of file JsonMappingReader.h.

Referenced by readMappingFromFile().

◆ mappingJsonSchemaString

const std::string mqtt::lib::JsonMappingReader::mappingJsonSchemaString
staticprivate

Definition at line 63 of file JsonMappingReader.h.


The documentation for this class was generated from the following files: