Olympe Engine 2.0
2D Game Engine with ECS Architecture
Loading...
Searching...
No Matches
TiledJsonHelper.h
Go to the documentation of this file.
1/*
2 * TiledJsonHelper.h - JSON parsing utilities for Tiled format
3 *
4 * Safe accessors and helpers for working with nlohmann::json
5 * when parsing Tiled map files.
6 */
7
8#pragma once
9
10#include "../../third_party/nlohmann/json.hpp"
11#include <string>
12#include <vector>
13
14namespace Olympe {
15namespace Tiled {
16
18
19 // Safe string accessor with default
20 inline std::string GetString(const json& j, const std::string& key, const std::string& defaultValue = "")
21 {
22 if (j.contains(key) && j[key].is_string()) {
23 return j[key].get<std::string>();
24 }
25 return defaultValue;
26 }
27
28 // Safe int accessor with default
29 inline int GetInt(const json& j, const std::string& key, int defaultValue = 0)
30 {
31 if (j.contains(key) && j[key].is_number_integer()) {
32 return j[key].get<int>();
33 }
34 return defaultValue;
35 }
36
37 // Safe float accessor with default
38 inline float GetFloat(const json& j, const std::string& key, float defaultValue = 0.0f)
39 {
40 if (j.contains(key) && j[key].is_number()) {
41 return static_cast<float>(j[key].get<double>());
42 }
43 return defaultValue;
44 }
45
46 // Safe double accessor with default
47 inline double GetDouble(const json& j, const std::string& key, double defaultValue = 0.0)
48 {
49 if (j.contains(key) && j[key].is_number()) {
50 return j[key].get<double>();
51 }
52 return defaultValue;
53 }
54
55 // Safe bool accessor with default
56 inline bool GetBool(const json& j, const std::string& key, bool defaultValue = false)
57 {
58 if (j.contains(key) && j[key].is_boolean()) {
59 return j[key].get<bool>();
60 }
61 return defaultValue;
62 }
63
64 // Safe array accessor
65 inline json GetArray(const json& j, const std::string& key)
66 {
67 if (j.contains(key) && j[key].is_array()) {
68 return j[key];
69 }
70 return json::array();
71 }
72
73 // Safe object accessor
74 inline json GetObject(const json& j, const std::string& key)
75 {
76 if (j.contains(key) && j[key].is_object()) {
77 return j[key];
78 }
79 return json::object();
80 }
81
82 // Check if key exists
83 inline bool HasKey(const json& j, const std::string& key)
84 {
85 return j.contains(key);
86 }
87
88 // Get array size
89 inline size_t GetArraySize(const json& j)
90 {
91 if (j.is_array()) {
92 return j.size();
93 }
94 return 0;
95 }
96
97 // Convert hex color string to int (e.g., "#AARRGGBB" or "#RRGGBB")
98 inline int ParseColor(const std::string& colorStr)
99 {
100 if (colorStr.empty() || colorStr[0] != '#') {
101 return 0xFFFFFFFF; // Default white
102 }
103
104 std::string hex = colorStr.substr(1);
105 unsigned long value = 0;
106
107 try {
108 value = std::stoul(hex, nullptr, 16);
109 } catch (...) {
110 return 0xFFFFFFFF;
111 }
112
113 // If 6 digits (RGB), add full alpha
114 if (hex.length() == 6) {
115 value |= 0xFF000000;
116 }
117
118 return static_cast<int>(value);
119 }
120
121 // Convert int color to hex string
122 inline std::string ColorToString(int color)
123 {
124 constexpr size_t BUFFER_SIZE = 16;
125 char buffer[BUFFER_SIZE];
126 std::snprintf(buffer, BUFFER_SIZE, "#%08X", static_cast<unsigned int>(color));
127 return std::string(buffer);
128 }
129
130} // namespace Tiled
131} // namespace Olympe
ComponentTypeID GetComponentTypeID_Static()
Definition ECS_Entity.h:56
size_t GetArraySize(const json &j)
json GetObject(const json &j, const std::string &key)
double GetDouble(const json &j, const std::string &key, double defaultValue=0.0)
nlohmann::json json
std::string GetString(const json &j, const std::string &key, const std::string &defaultValue="")
int GetInt(const json &j, const std::string &key, int defaultValue=0)
bool HasKey(const json &j, const std::string &key)
bool GetBool(const json &j, const std::string &key, bool defaultValue=false)
json GetArray(const json &j, const std::string &key)
std::string ColorToString(int color)
float GetFloat(const json &j, const std::string &key, float defaultValue=0.0f)
int ParseColor(const std::string &colorStr)
nlohmann::json json