-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathJSONHelpers.h
More file actions
159 lines (139 loc) · 3.32 KB
/
JSONHelpers.h
File metadata and controls
159 lines (139 loc) · 3.32 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
* Copyright (c) [2025-2026] [Zhao Song]
*
* Internal header for JSON parsing helper functions.
* Used by Column.cc, Index.cc, and Table.cc for SDI parsing.
*/
#ifndef JSONHELPERS_H_
#define JSONHELPERS_H_
#include <rapidjson/document.h>
#include <string>
#include <algorithm>
#include <cstdint>
namespace ibd_ninja {
// Template functions for reading values from RapidJSON
template <typename GV>
bool ReadValue(bool* ap, const GV& gv) {
if (!gv.IsBool()) {
return false;
}
*ap = gv.GetBool();
return true;
}
template <typename GV>
bool ReadValue(int32_t* ap, const GV& gv) {
if (!gv.IsInt()) {
return false;
}
*ap = gv.GetInt();
return true;
}
template <typename GV>
bool ReadValue(uint32_t* ap, const GV& gv) {
if (!gv.IsUint()) {
return false;
}
*ap = gv.GetUint();
return true;
}
template <typename GV>
bool ReadValue(int64_t* ap, const GV& gv) {
if (!gv.IsInt64()) {
return false;
}
*ap = gv.GetInt64();
return true;
}
template <typename GV>
bool ReadValue(uint64_t* ap, const GV& gv) {
if (!gv.IsUint64()) {
return false;
}
*ap = gv.GetUint64();
return true;
}
template <typename GV>
bool ReadValue(std::string* ap, const GV& gv) {
if (!gv.IsString()) {
return false;
}
*ap = gv.GetString();
return true;
}
template <typename T, typename GV>
bool Read(T* ap, const GV& gv, const char* key) {
if (!gv.HasMember(key)) {
return false;
}
return ReadValue(ap, gv[key]);
}
template <typename ENUM_T, typename GV>
bool ReadEnum(ENUM_T* ap, const GV& gv, const char* key) {
uint64_t v = 0;
if (!Read(&v, gv, key)) {
return false;
}
*ap = static_cast<ENUM_T>(v);
return true;
}
// GetValue functions for Properties parsing
inline bool GetValue(const std::string& value_str, std::string* value) {
*value = value_str;
return true;
}
inline bool GetValue(const std::string& value_str, bool* value) {
if (value_str == "true") {
*value = true;
return true;
}
if (value_str == "false" || value_str == "0") {
*value = false;
return true;
}
size_t pos = 0;
if (value_str[pos] == '+' || value_str[pos] == '-') {
pos += 1;
}
bool is_digit = std::all_of(value_str.begin() + pos, value_str.end(),
::isdigit);
if (is_digit) {
*value = true;
} else {
return false;
}
return true;
}
inline bool GetValue(const std::string& value_str, int32_t* value) {
*value = strtol(value_str.c_str(), nullptr, 10);
return true;
}
inline bool GetValue(const std::string& value_str, uint32_t* value) {
*value = strtoul(value_str.c_str(), nullptr, 10);
return true;
}
inline bool GetValue(const std::string& value_str, int64_t* value) {
*value = strtoll(value_str.c_str(), nullptr, 10);
return true;
}
inline bool GetValue(const std::string& value_str, uint64_t* value) {
*value = strtoull(value_str.c_str(), nullptr, 10);
return true;
}
template <typename ENUM_T>
bool GetValue(const std::string& value_str, ENUM_T* value) {
uint64_t v = 0;
GetValue(value_str, &v);
*value = static_cast<ENUM_T>(v);
return true;
}
template <typename PP, typename GV>
bool ReadProperties(PP* pp, const GV& gv, const char* key) {
std::string opt_string;
if (Read(&opt_string, gv, key)) {
return pp->InsertValues(opt_string);
} else {
return false;
}
}
} // namespace ibd_ninja
#endif // JSONHELPERS_H_