Brunot
Loading...
Searching...
No Matches
Json.h
Go to the documentation of this file.
1// File: Json.h
2// Description: JSON file parser and handler with all declarations, methods, variables, and templates
3// Author(s): Marcelo Escamilla (marcelo.escamilla@digipen.edu)
4// 2025 / 09 / 19
5// (C) Digipen 2025
6// ____ __ __ __
7// /\__ _\/\ \ /\ \/\ \
8// \/_/\ \/\ \ \___ __ \ \ \_\ \ ___ __ __ ____ __
9// \ \ \ \ \ _ `\ /'__`\ \ \ _ \ / __`\/\ \/\ \ /',__\ /'__`\
10// \ \ \ \ \ \ \ \/\ __/ \ \ \ \ \/\ \L\ \ \ \_\ \/\__, `\/\ __/
11// \ \_\ \ \_\ \_\ \____\ \ \_\ \_\ \____/\ \____/\/\____/\ \____\
12// \/_/ \/_/\/_/\/____/ \/_/\/_/\/___/ \/___/ \/___/ \/____/
13#pragma once
14#include <string_view>
15#include "Logging.h"
16//#include <nlohmann/json_fwd.hpp>
17#include <nlohmann/json.hpp>
18#include <string.h>
19
20
21using json = nlohmann::json;
22
24//using value_type = nlohmann::basic_json;
25
27//using reference = value_type&;
28
29namespace sys
30{
31class Json
32{
33
34public:
40 Json(std::string filePath);
41 Json(json file);
42 Json() = default;
43 Json(const Json&) = default;
44 Json(Json&&) = default;
45 ~Json() = default;
46
47 // dont use for now
48 auto data() -> json&;
49
50 auto data() const -> const json&;
51
52 static auto test() -> void; // for testing the read function
53 static auto savingTest() -> void; // for testing the save function
54
55 // Dump the JSON to the console
56 // pretty = sets the indentation level for pretty printing (usually 4)
57 auto jsonDump(int pretty) -> void;
58
59 // Read a value from the JSON using a dot-separated key path
60 // Example: "tester.value"
61 // tester is the root/the json object name, value is the object inside tester that you want to go into
62 // continuing adding "." will go deeper into the json structure
63 // value is the variable you want to store the result in
64 template <typename T>
65 auto read(const std::string& keyPath, T& value) const -> bool
66 {
67 assert(isValid && "Stream is not valid. Ensure the file was opened successfully.");
68
69 try
70 {
71 auto current = &j;
72 size_t start = 0, end;
73
74 while ((end = keyPath.find('.', start)) != std::string::npos)
75 {
76 auto key = keyPath.substr(start, end - start);
77 current = &current->at(key);
78 start = end + 1;
79 }
80
81 auto lastKey = keyPath.substr(start);
82 value = current->at(lastKey).get<T>();
83 }
84 catch (const json::exception& exception)
85 {
86 hlg::Error("Stream: JSON key error: {}", exception.what());
87 return false;
88 }
89
90 return true;
91 }
92
97 auto clear() -> void;
98
103 auto save() -> void;
104
110 auto saveAs(const std::string& newFilePath) -> void;
111
112 // Overload the subscript operator to access JSON elements directly
113 template <typename T>
114 auto operator[](T* key)
115 {
116 return j[key];
117 }
118
119
120 // private
121private:
122 bool isValid{false};
124 std::string filePath;
125
132 auto open(const std::string& filePath) -> bool;
133
134};
135
136} // namespace th
nlohmann::json json
Definition Json.cpp:19
std::string filePath
Definition Json.h:124
auto operator[](T *key)
Definition Json.h:114
auto data() -> json &
Definition Json.cpp:72
auto open(const std::string &filePath) -> bool
Definition Json.cpp:37
bool isValid
Definition Json.h:122
auto jsonDump(int pretty) -> void
Definition Json.cpp:116
Json(std::string filePath)
Constructor that opens and parses a JSON file.
Definition Json.cpp:22
json j
Definition Json.h:123
Json(Json &&)=default
~Json()=default
auto read(const std::string &keyPath, T &value) const -> bool
Definition Json.h:65
Json(const Json &)=default
static auto savingTest() -> void
Definition Json.cpp:186
Json()=default
auto save() -> void
Definition Json.cpp:121
auto saveAs(const std::string &newFilePath) -> void
Definition Json.cpp:157
auto clear() -> void
Definition Json.cpp:179
auto Error(spdlog::format_string_t< Args... > message, Args &&... args) -> void
Definition Logging.h:110
the type of elements in a basic_json container
Definition GameObject.h:32
Definition Entity.h:304