FIMS  v0.9.3
Loading...
Searching...
No Matches
fims_json.hpp
Go to the documentation of this file.
1#ifndef FIMS_JSON_HPP
2#define FIMS_JSON_HPP
3
13#include <cctype>
14#include <iostream>
15#include <fstream>
16#include <map>
17#include <sstream>
18#include <string>
19#include <algorithm>
20#include <vector>
21
22namespace fims {
23class JsonValue;
24
28using JsonObject = std::map<std::string, JsonValue>;
29
33using JsonArray = std::vector<JsonValue>;
34
46
50class JsonValue {
51 public:
54
56 JsonValue(int num) : type(JsonValueType::Number), number(num) {}
57
59 JsonValue(double num) : type(JsonValueType::Number), number(num) {}
60
62 JsonValue(const std::string& str) : type(JsonValueType::String), str(str) {}
63
65 JsonValue(bool b) : type(JsonValueType::Bool), boolean(b) {}
66
68 JsonValue(const JsonObject& obj) : type(JsonValueType::Object), object(obj) {}
69
71 JsonValue(const JsonArray& arr) : type(JsonValueType::JArray), array(arr) {}
72
74 JsonValueType GetType() const { return type; }
75
77 int GetInt() const { return static_cast<int>(number); }
78
80 double GetDouble() const { return number; }
81
83 const std::string& GetString() const { return str; }
84
86 bool GetBool() const { return boolean; }
87
89 JsonObject& GetObject() { return object; }
90
92 JsonArray& GetArray() { return array; }
93
94 private:
95 JsonValueType type;
96 double number;
97 std::string str;
98 bool boolean;
99 JsonObject object;
100 JsonArray array;
101};
102
107 public:
109 JsonValue Parse(const std::string& json);
111 void WriteToFile(const std::string& filename, JsonValue jsonValue);
114
116 static std::string removeWhitespace(const std::string& input) {
117 std::string result = input;
118 result.erase(std::remove_if(result.begin(), result.end(), ::isspace),
119 result.end());
120 return result;
121 }
122
128 static std::string PrettyFormatJSON(const std::string& json) {
129 std::string result;
131 int indentLevel = 0;
132 bool inQuotes = false;
133
134 for (size_t i = 0; i < input.size(); ++i) {
135 char current = input[i];
136
137 switch (current) {
138 case '{':
139 case '[':
140 result += current;
141 if (!inQuotes) {
142 result += '\n';
143 indentLevel++;
144 result += std::string(indentLevel * 4, ' ');
145 }
146 break;
147
148 case '}':
149 case ']':
150 if (!inQuotes) {
151 result += '\n';
152 indentLevel--;
153 result += std::string(indentLevel * 4, ' ');
154 }
155 result += current;
156 break;
157
158 case ',':
159 result += current;
160 if (!inQuotes) {
161 result += '\n';
162 result += std::string(indentLevel * 4, ' ');
163 }
164 break;
165
166 case ':':
167 result += current;
168 if (!inQuotes) result += " ";
169 break;
170
171 case '"':
172 result += current;
173 // Toggle inQuotes when we encounter a double-quote
174 if (i == 0 || input[i - 1] != '\\') {
176 }
177 break;
178
179 default:
180 result += current;
181 break;
182 }
183 }
184 return result;
185 }
186
187 private:
189 void SkipWhitespace();
191 JsonValue ParseValue();
193 JsonValue ParseNumber();
195 JsonValue ParseString();
197 JsonValue ParseBool();
199 JsonValue ParseNull();
201 JsonValue ParseObject();
203 JsonValue ParseArray();
205 void WriteJsonValue(std::ofstream& outputFile, JsonValue jsonValue);
207 void PrintJsonValue(std::ostream& outputFile, JsonValue jsonValue);
209 void Indent(std::ostream& outputFile, int level);
211 void Indent(std::ofstream& outputFile, int level);
212
213 std::string data;
214 size_t position;
215};
216
217} // namespace fims
218#endif
Definition fims_json.hpp:106
void Show(JsonValue jsonValue)
Definition fims_json.cpp:295
void WriteToFile(const std::string &filename, JsonValue jsonValue)
Definition fims_json.cpp:223
static std::string PrettyFormatJSON(const std::string &json)
Formats a JSON string.
Definition fims_json.hpp:128
JsonValue Parse(const std::string &json)
Definition fims_json.cpp:19
static std::string removeWhitespace(const std::string &input)
Definition fims_json.hpp:116
Definition fims_json.hpp:50
JsonValue(bool b)
Definition fims_json.hpp:65
JsonValue()
Definition fims_json.hpp:53
JsonValue(const JsonArray &arr)
Definition fims_json.hpp:71
double GetDouble() const
Definition fims_json.hpp:80
JsonArray & GetArray()
Definition fims_json.hpp:92
const std::string & GetString() const
Definition fims_json.hpp:83
JsonValue(const JsonObject &obj)
Definition fims_json.hpp:68
JsonValue(double num)
Definition fims_json.hpp:59
int GetInt() const
Definition fims_json.hpp:77
JsonObject & GetObject()
Definition fims_json.hpp:89
bool GetBool() const
Definition fims_json.hpp:86
JsonValue(const std::string &str)
Definition fims_json.hpp:62
JsonValueType GetType() const
Definition fims_json.hpp:74
JsonValue(int num)
Definition fims_json.hpp:56
std::vector< JsonValue > JsonArray
Definition fims_json.hpp:33
JsonValueType
Definition fims_json.hpp:38
@ Bool
Definition fims_json.hpp:42
@ Number
Definition fims_json.hpp:40
@ String
Definition fims_json.hpp:41
@ Object
Definition fims_json.hpp:43
@ Null
Definition fims_json.hpp:39
@ JArray
Definition fims_json.hpp:44
std::map< std::string, JsonValue > JsonObject
Definition fims_json.hpp:28
void clear_internal()
Clears the internal objects.
Definition rcpp_interface.hpp:235