|
| 1 | +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
| 2 | +* * |
| 3 | + * Ghost, a micro-kernel based operating system for the x86 architecture * |
| 4 | + * Copyright (C) 2025, Max Schlüssel <[email protected]> * |
| 5 | + * * |
| 6 | + * This program is free software: you can redistribute it and/or modify * |
| 7 | + * it under the terms of the GNU General Public License as published by * |
| 8 | + * the Free Software Foundation, either version 3 of the License, or * |
| 9 | + * (at your option) any later version. * |
| 10 | + * * |
| 11 | + * This program is distributed in the hope that it will be useful, * |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 | + * GNU General Public License for more details. * |
| 15 | + * * |
| 16 | + * You should have received a copy of the GNU General Public License * |
| 17 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. * |
| 18 | + * * |
| 19 | + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ |
| 20 | + |
| 21 | +#ifndef LIBJSON_JSON_NODE |
| 22 | +#define LIBJSON_JSON_NODE |
| 23 | + |
| 24 | +#include <string> |
| 25 | +#include <variant> |
| 26 | +#include <vector> |
| 27 | +#include <map> |
| 28 | + |
| 29 | +struct g_json_node |
| 30 | +{ |
| 31 | + using object = std::map<std::string, g_json_node>; |
| 32 | + using array = std::vector<g_json_node>; |
| 33 | + using value = std::variant<std::nullptr_t, bool, double, std::string, array, object>; |
| 34 | + |
| 35 | + value v{}; |
| 36 | + |
| 37 | + g_json_node() : v(nullptr) |
| 38 | + { |
| 39 | + } |
| 40 | + |
| 41 | + g_json_node(std::nullptr_t) : v(nullptr) |
| 42 | + { |
| 43 | + } |
| 44 | + |
| 45 | + g_json_node(bool b) : v(b) |
| 46 | + { |
| 47 | + } |
| 48 | + |
| 49 | + g_json_node(double d) : v(d) |
| 50 | + { |
| 51 | + } |
| 52 | + |
| 53 | + g_json_node(const char* s) : v(std::string(s)) |
| 54 | + { |
| 55 | + } |
| 56 | + |
| 57 | + g_json_node(std::string s) : v(std::move(s)) |
| 58 | + { |
| 59 | + } |
| 60 | + |
| 61 | + g_json_node(array a) : v(std::move(a)) |
| 62 | + { |
| 63 | + } |
| 64 | + |
| 65 | + g_json_node(object o) : v(std::move(o)) |
| 66 | + { |
| 67 | + } |
| 68 | + |
| 69 | + bool isObject() const { return std::holds_alternative<object>(v); } |
| 70 | + bool isArray() const { return std::holds_alternative<array>(v); } |
| 71 | + bool isString() const { return std::holds_alternative<std::string>(v); } |
| 72 | + bool isNumber() const { return std::holds_alternative<double>(v); } |
| 73 | + bool isBool() const { return std::holds_alternative<bool>(v); } |
| 74 | + bool isNull() const { return std::holds_alternative<std::nullptr_t>(v); } |
| 75 | + |
| 76 | + object& asObject() { return std::get<object>(v); } |
| 77 | + array& asArray() { return std::get<array>(v); } |
| 78 | + std::string& asString() { return std::get<std::string>(v); } |
| 79 | + double& asNumber() { return std::get<double>(v); } |
| 80 | + bool& asBool() { return std::get<bool>(v); } |
| 81 | +}; |
| 82 | + |
| 83 | +#endif |
0 commit comments