Webdar 1.0.0
Web user interface to libdar
jsoner.hpp
Go to the documentation of this file.
1/*********************************************************************/
2// webdar - a web server and interface program to libdar
3// Copyright (C) 2013-2025 Denis Corbin
4//
5// This file is part of Webdar
6//
7// Webdar is free software: you can redistribute it and/or modify
8// it under the terms of the GNU General Public License as published by
9// the Free Software Foundation, either version 3 of the License, or
10// (at your option) any later version.
11//
12// Webdar is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15// GNU General Public License for more details.
16//
17// You should have received a copy of the GNU General Public License
18// along with Webdar. If not, see <http://www.gnu.org/licenses/>
19//
20//----
21// to contact the author: dar.linux@free.fr
22/*********************************************************************/
23
24#ifndef JSONER_HPP
25#define JSONER_HPP
26
27 // C system header files
28#include "my_config.h"
29extern "C"
30{
31
32}
33
34 // C++ system header files
35#include <dar/libdar.hpp>
36#include <string>
37
38#if HAVE_NLOHMANN_JSON_HPP
39#include <nlohmann/json.hpp>
40#endif
41
42 // webdar headers
43#include "exceptions.hpp"
44
46
47using json = nlohmann::json;
48
49
51
53
55{
56public:
57 exception_json(const json::exception & e): exception_base(e.what()) {};
58 exception_json(const std::string & context, const json::exception & e):
59 exception_base(context + ": " + e.what()) {};
60 exception_json(const std::string & s): exception_base(s) {};
61
62 virtual exception_base *clone() const override { return cloner<exception_json>((void *)this); };
63};
64
65
67
71
72class jsoner
73{
74public:
75 jsoner() = default;
76 jsoner(const jsoner & ref) = default;
77 jsoner(jsoner && ref) noexcept(false) = default;
78 jsoner & operator = (const jsoner & ref) = default;
79 jsoner & operator = (jsoner && ref) noexcept(false) = default;
80 virtual ~jsoner() = default;
81
83
87 virtual void load_json(const json & source) = 0;
88
90
98 virtual json save_json() const = 0;
99
100
102 virtual void clear_json() = 0;
103
104protected:
105
107
112 static json wrap_config_with_json_header(unsigned int version,
113 const std::string & class_id,
114 const json & config);
115
116
118
124 static json unwrap_config_from_json_header(const json & source,
125 unsigned int & version,
126 std::string & class_id);
127
128 static constexpr const char* jsoner_version_label = "version";
129 static constexpr const char* jsoner_id_label = "class_id";
130 static constexpr const char* jsoner_config_label = "config";
131};
132
133#endif
pure virtual class parent of all webdar exceptions
Definition: exceptions.hpp:47
class exception_json
Definition: jsoner.hpp:55
class jsoner
Definition: jsoner.hpp:73
virtual void load_json(const json &source)=0
setup the components from the json provided information
virtual json save_json() const =0
produce a json structure from the component configuration
static json unwrap_config_from_json_header(const json &source, unsigned int &version, std::string &class_id)
from a given json global and common json structure split header parts and return the config part
Definition: jsoner.cpp:63
static json wrap_config_with_json_header(unsigned int version, const std::string &class_id, const json &config)
given a version, class_id and configuration generates the global and common json structure
Definition: jsoner.cpp:43
virtual void clear_json()=0
instruct the object to get to its default/initial configuration