-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.cpp
41 lines (30 loc) · 1.03 KB
/
common.cpp
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
#include "common.h"
#include <QJsonObject>
#include <QVariant>
#include <fmt/format.h>
namespace El {
// -----------------------------------------------------------------------------
auto Price::from_json(QJsonObject const &json) -> Price
{
using namespace Qt::Literals::StringLiterals;
if (!json.contains(u"timestamp"_s)) {
throw Exception{"Missing 'timestamp' element"};
}
auto const t = json.value(u"timestamp"_s);
bool ok = false;
auto const timestamp = t.toVariant().toLongLong(&ok);
if (!ok) {
throw Exception{fmt::format("Invalid 'timestamp' element value '{}'", t.toString())};
}
if (!json.contains(u"price"_s)) {
throw Exception{"Missing 'price' element"};
}
auto const p = json.value(u"price"_s);
ok = false;
auto const price = p.toVariant().toDouble(&ok);
if (!ok) {
throw Exception{fmt::format("Invalid 'price' element value '{}'", p.toString())};
}
return Price{static_cast<int>(timestamp / SECS_IN_HOUR), price};
}
} // namespace El