Skip to content

Commit 5d7ab55

Browse files
Updating current version of example2.cpp...
error: out-of-line definition of 'get' does not match any declaration in 'simdjson::fallback::ondemand::value' 45 | simdjson_inline simdjson_result<T> simdjson::ondemand::value::get() noexcept { |
1 parent 2680090 commit 5d7ab55

1 file changed

Lines changed: 105 additions & 26 deletions

File tree

examples/example2.cpp

Lines changed: 105 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,33 @@
66
#include <print>
77
#include <string>
88
#include <vector>
9+
#include <type_traits>
910
using namespace simdjson;
1011

12+
13+
struct MyStruct {
14+
int id;
15+
std::string name;
16+
std::vector<int> values;
17+
};
18+
19+
struct Y {
20+
int g;
21+
std::string h;
22+
std::vector<int> i;
23+
};
24+
25+
struct X {
26+
char a;
27+
int b;
28+
int c;
29+
std::string d;
30+
std::vector<int> e;
31+
std::vector<std::string> f;
32+
Y y;
33+
};
34+
35+
/*
1136
// todo: use reflection to generate
1237
template <>
1338
simdjson_inline simdjson_result<std::vector<int>>
@@ -48,15 +73,83 @@ simdjson::ondemand::value::get() noexcept {
4873
vec.push_back(t);
4974
}
5075
return vec;
76+
}*/
77+
78+
template <typename T>
79+
concept PushableContainer =
80+
requires(T a, typename T::value_type val) {
81+
a.push_back(val);
82+
} && !std::is_same_v<T, std::string> &&
83+
!std::is_same_v<T, std::string_view> &&
84+
!std::is_same_v<T, const char*>;
85+
86+
template <typename T>
87+
requires PushableContainer<T>
88+
simdjson_inline simdjson_result<T> simdjson::ondemand::value::get() noexcept {
89+
ondemand::array array;
90+
auto error = get_array().get(array);
91+
if (error) {
92+
return error;
93+
}
94+
T container;
95+
for (auto v : array) {
96+
typename T::value_type val;
97+
if constexpr (std::is_same_v<typename T::value_type, double>) {
98+
error = v.get_double().get(val);
99+
} else if constexpr (std::is_same_v<typename T::value_type, bool>) {
100+
error = v.get_bool().get(val);
101+
} else if constexpr (std::is_same_v<typename T::value_type, uint64_t>) {
102+
error = v.get_uint64().get(val);
103+
} else if constexpr (std::is_same_v<typename T::value_type, int64_t>) {
104+
error = v.get_int64().get(val);
105+
} else if constexpr (std::is_same_v<typename T::value_type, int>) {
106+
int64_t temp_val;
107+
error = v.get_int64().get(temp_val);
108+
if (!error) {
109+
val = static_cast<int>(temp_val);
110+
}
111+
} else if constexpr (std::is_same_v<typename T::value_type, std::string>) {
112+
std::string str_val;
113+
error = v.get_string(str_val);
114+
if (!error) {
115+
val = std::move(str_val);
116+
}
117+
} else if constexpr (std::is_same_v<typename T::value_type, std::string_view>) {
118+
std::string_view str_view_val;
119+
error = v.get_string(str_view_val, false);
120+
if (!error) {
121+
val = str_view_val;
122+
}
123+
} else if constexpr (std::is_same_v<typename T::value_type, simdjson::ondemand::raw_json_string>) {
124+
simdjson::ondemand::raw_json_string raw_val;
125+
error = v.get_raw_json_string().get(raw_val);
126+
if (!error) {
127+
val = raw_val;
128+
}
129+
} else {
130+
static_assert(!sizeof(T), "Unsupported value type in the container.");
131+
}
132+
133+
if (error) {
134+
return error;
135+
}
136+
container.push_back(std::move(val));
137+
}
138+
return container;
51139
}
52140

53-
struct MyStruct {
54-
int id;
55-
std::string name;
56-
std::vector<int> values;
57-
};
141+
// Specialization for std::vector<int>
142+
template <>
143+
simdjson_inline simdjson_result<std::vector<int> > simdjson::ondemand::value::get() noexcept {
144+
return get<std::vector<int>>();
145+
}
146+
147+
// Specialization for std::vector<std::string>
148+
template <>
149+
simdjson_inline simdjson_result<std::vector<std::string> > simdjson::ondemand::value::get() noexcept {
150+
return get<std::vector<std::string>>();
151+
}
58152

59-
// todo: use reflection to generate
60153
template <>
61154
simdjson_inline simdjson_result<MyStruct>
62155
simdjson::ondemand::value::get() noexcept {
@@ -123,7 +216,7 @@ simdjson::ondemand::document::get() & noexcept {
123216
return error;
124217
}
125218
} else if (key == "values") {
126-
error = field.value().get<std::vector<int>>().get(s.values);
219+
error = field.value().get<std::vector<int>>().get(s.values);
127220
if (error) {
128221
return error;
129222
}
@@ -132,12 +225,6 @@ simdjson::ondemand::document::get() & noexcept {
132225
return s;
133226
}
134227

135-
struct Y {
136-
int g;
137-
std::string h;
138-
std::vector<int> i;
139-
};
140-
141228
// todo: use reflection to generate
142229
template <>
143230
simdjson_inline simdjson_result<Y> simdjson::ondemand::value::get() noexcept {
@@ -172,16 +259,6 @@ simdjson_inline simdjson_result<Y> simdjson::ondemand::value::get() noexcept {
172259
return y;
173260
}
174261

175-
struct X {
176-
char a;
177-
int b;
178-
int c;
179-
std::string d;
180-
std::vector<int> e;
181-
std::vector<std::string> f;
182-
Y y;
183-
};
184-
185262
// todo: use reflection to generate
186263
template <>
187264
simdjson_inline simdjson_result<X> simdjson::ondemand::value::get() noexcept {
@@ -231,7 +308,7 @@ simdjson_inline simdjson_result<X> simdjson::ondemand::value::get() noexcept {
231308
return error;
232309
}
233310
} else if (key == "y") {
234-
error = field.value().get<Y>().get(x.y);
311+
// error = field.value().get<Y>().get(x.y);
235312
if (error) {
236313
return error;
237314
}
@@ -290,7 +367,7 @@ simdjson::ondemand::document::get() & noexcept {
290367
return error;
291368
}
292369
} else if (key == "y") {
293-
error = field.value().get<Y>().get(x.y);
370+
// error = field.value().get<Y>().get(x.y);
294371
if (error) {
295372
return error;
296373
}
@@ -299,6 +376,7 @@ simdjson::ondemand::document::get() & noexcept {
299376
return x;
300377
}
301378

379+
302380
int main() {
303381
std::string json_str = R"({"id": 1, "name": "example", "values": [1, 2, 3]})";
304382
ondemand::parser parser;
@@ -309,14 +387,15 @@ int main() {
309387
simdjson::json_builder::fast_to_json_string(sb, my_struct);
310388
std::cout << sb.c_str() << std::endl;
311389

390+
/*
312391
std::string json_str_nested =
313392
R"({"a":1,"b":10,"c":0,"d":"test string\n\r\"","e":[1,2,3],"f":["ab","cd","fg"],"y":{"g":100,"h":"test string\n\r\"","i":[1,2,3]}})";
314393
doc = parser.iterate(json_str_nested);
315394
316395
X s1 = X(doc);
317396
simdjson::json_builder::StringBuilder sb2;
318397
simdjson::json_builder::fast_to_json_string(sb2, s1);
319-
std::cout << sb2.c_str() << std::endl;
398+
std::cout << sb2.c_str() << std::endl;*/
320399

321400
return 0;
322401
}

0 commit comments

Comments
 (0)