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() = default;
42 Json(const Json&) = default;
43 Json(Json&&) = default;
44 ~Json() = default;
45
46 // dont use for now
47 auto data() -> json&;
48
49 static auto test() -> void; // for testing the read function
50 static auto savingTest() -> void; // for testing the save function
51
52 // Dump the JSON to the console
53 // pretty = sets the indentation level for pretty printing (usually 4)
54 auto jsonDump(int pretty) -> void;
55
56 // Read a value from the JSON using a dot-separated key path
57 // Example: "tester.value"
58 // tester is the root/the json object name, value is the object inside tester that you want to go into
59 // continuing adding "." will go deeper into the json structure
60 // value is the variable you want to store the result in
61 template <typename T>
62 auto read(const std::string& keyPath, T& value) const -> bool
63 {
64 assert(isValid && "Stream is not valid. Ensure the file was opened successfully.");
65
66 try
67 {
68 auto current = &j;
69 size_t start = 0, end;
70
71 while ((end = keyPath.find('.', start)) != std::string::npos)
72 {
73 auto key = keyPath.substr(start, end - start);
74 current = &current->at(key);
75 start = end + 1;
76 }
77
78 auto lastKey = keyPath.substr(start);
79 value = current->at(lastKey).get<T>();
80 }
81 catch (const json::exception& exception)
82 {
83 hlg::Error("Stream: JSON key error: {}", exception.what());
84 return false;
85 }
86
87 return true;
88 }
89
94 auto clear() -> void;
95
100 auto save() -> void;
101
107 auto saveAs(const std::string& newFilePath) -> void;
108
109 // Overload the subscript operator to access JSON elements directly
110 template <typename T>
111 auto operator[](T* key)
112 {
113 return j[key];
114 }
115
116
117 // private
118private:
119 bool isValid{false};
121 std::string filePath;
122
129 auto open(std::string filePath) -> bool;
130
131};
132
133} // namespace th
nlohmann::json json
Definition Json.cpp:19
std::string filePath
Definition Json.h:121
auto operator[](T *key)
Definition Json.h:111
auto data() -> json &
Definition Json.cpp:67
bool isValid
Definition Json.h:119
auto jsonDump(int pretty) -> void
Definition Json.cpp:106
Json(std::string filePath)
Constructor that opens and parses a JSON file.
Definition Json.cpp:22
json j
Definition Json.h:120
Json(Json &&)=default
~Json()=default
auto read(const std::string &keyPath, T &value) const -> bool
Definition Json.h:62
auto open(std::string filePath) -> bool
Definition Json.cpp:32
Json(const Json &)=default
static auto savingTest() -> void
Definition Json.cpp:176
Json()=default
auto save() -> void
Definition Json.cpp:111
auto saveAs(const std::string &newFilePath) -> void
Definition Json.cpp:147
auto clear() -> void
Definition Json.cpp:169
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:224