-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonParser.cpp
More file actions
59 lines (46 loc) · 1.47 KB
/
Copy pathJsonParser.cpp
File metadata and controls
59 lines (46 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include "JsonParser.h"
#include <sstream>
std::map<std::string, std::string> JsonParser::parseSimpleJson(const std::string &json)
{
std::map<std::string, std::string> result;
// Simple JSON parser for basic string fields
size_t pos = 0;
while (pos < json.length())
{
size_t keyStart = json.find('"', pos);
if (keyStart == std::string::npos)
break;
size_t keyEnd = json.find('"', keyStart + 1);
if (keyEnd == std::string::npos)
break;
std::string key = json.substr(keyStart + 1, keyEnd - keyStart - 1);
size_t colonPos = json.find(':', keyEnd);
if (colonPos == std::string::npos)
break;
size_t valueStart = json.find('"', colonPos);
if (valueStart == std::string::npos)
break;
size_t valueEnd = json.find('"', valueStart + 1);
if (valueEnd == std::string::npos)
break;
std::string value = json.substr(valueStart + 1, valueEnd - valueStart - 1);
result[key] = value;
pos = valueEnd + 1;
}
return result;
}
std::string JsonParser::createJsonResponse(const std::map<std::string, std::string> &data)
{
std::ostringstream json;
json << "{";
bool first = true;
for (const auto &pair : data)
{
if (!first)
json << ",";
json << "\"" << pair.first << "\":\"" << pair.second << "\"";
first = false;
}
json << "}";
return json.str();
}