forked from janhq/cortex.cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvariant_map.h
More file actions
62 lines (49 loc) · 1.46 KB
/
variant_map.h
File metadata and controls
62 lines (49 loc) · 1.46 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
#pragma once
#include <json/value.h>
#include <string>
#include <unordered_map>
#include <variant>
#include "utils/result.hpp"
namespace Cortex {
using ValueVariant = std::variant<std::string, bool, uint64_t, double>;
using VariantMap = std::unordered_map<std::string, ValueVariant>;
inline cpp::result<VariantMap, std::string> ConvertJsonValueToMap(
const Json::Value& json) {
VariantMap result;
if (!json.isObject()) {
return cpp::fail("Input json is not an object");
}
for (const auto& key : json.getMemberNames()) {
const Json::Value& value = json[key];
switch (value.type()) {
case Json::nullValue:
// Skip null values
break;
case Json::stringValue:
result.emplace(key, value.asString());
break;
case Json::booleanValue:
result.emplace(key, value.asBool());
break;
case Json::uintValue:
case Json::intValue:
// Handle both signed and unsigned integers
if (value.isUInt64()) {
result.emplace(key, value.asUInt64());
} else {
// Convert to double if the integer is negative or too large
result.emplace(key, value.asDouble());
}
break;
case Json::realValue:
result.emplace(key, value.asDouble());
break;
case Json::arrayValue:
case Json::objectValue:
// currently does not handle complex type
break;
}
}
return result;
}
}; // namespace Cortex