Skip to content

Commit bd76357

Browse files
committed
mixing with custom
1 parent 3ee3c41 commit bd76357

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

cppcon2025/software/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ struct Player {
1515
## Use docker
1616
1717
```sh
18-
./run_docker.sh
18+
./run_docker.sh bash
1919
```
2020

2121
## Install OpenSLL

cppcon2025/software/webservice.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,51 @@ std::string grab_weather_data(const std::string& latitude, const std::string& lo
4343
return response_data;
4444
}
4545

46+
47+
/**
48+
* Next we check that we can combine custom types with static reflection.
49+
*/
50+
51+
52+
class MyDate {
53+
public:
54+
void assign(std::string_view str) {
55+
date_str = str;
56+
}
57+
const std::string& to_string() const {
58+
return date_str;
59+
}
60+
private:
61+
std::string date_str;
62+
};
63+
64+
namespace simdjson {
65+
template <typename simdjson_value>
66+
auto tag_invoke(deserialize_tag, simdjson_value &val, MyDate& date) {
67+
std::string_view str;
68+
auto error = val.get_string().get(str);
69+
if(error) { return error; }
70+
date.assign(str);
71+
return simdjson::SUCCESS;
72+
}
73+
} // namespace simdjson
74+
75+
struct complicated_weather_data {
76+
std::vector<MyDate> time;
77+
std::vector<float> temperature_2m;
78+
std::vector<float> relative_humidity_2m;
79+
std::vector<float> winddirection_10m;
80+
std::vector<float> precipitation;
81+
std::vector<float> windspeed_10m;
82+
};
83+
84+
4685
int main() {
4786
std::string weather_data_str = grab_weather_data("45.5017", "-73.5673");
4887
simdjson::ondemand::parser parser;
4988
simdjson::ondemand::document doc = parser.iterate(simdjson::pad(weather_data_str));
89+
90+
// If it is simple enough, static reflection works fine.
5091
weather_data wd = doc["hourly"].get<weather_data>();
5192
// Assuming all vectors have the same length
5293
for (size_t i = 0; i < wd.time.size(); ++i) {
@@ -58,5 +99,18 @@ int main() {
5899
wd.precipitation[i],
59100
wd.windspeed_10m[i]);
60101
}
102+
103+
// It won't work with MyDate, so we need to have a custom deserializer.
104+
// complicated weather data
105+
complicated_weather_data cwd = doc["hourly"].get<complicated_weather_data>();
106+
for (size_t i = 0; i < cwd.time.size(); ++i) {
107+
fmt::print("CWD Time: {}, Temperature: {:.1f}°C, Humidity: {:.1f}%, Wind Direction: {:.1f}°, Precipitation: {:.1f}mm, Wind Speed: {:.1f}km/h\n",
108+
cwd.time[i].to_string(),
109+
cwd.temperature_2m[i],
110+
cwd.relative_humidity_2m[i],
111+
cwd.winddirection_10m[i],
112+
cwd.precipitation[i],
113+
cwd.windspeed_10m[i]);
114+
}
61115
return EXIT_SUCCESS;
62116
}

0 commit comments

Comments
 (0)