forked from memgraph/memgraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert.cpp
More file actions
124 lines (105 loc) · 4.32 KB
/
Copy pathconvert.cpp
File metadata and controls
124 lines (105 loc) · 4.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
// Copyright 2026 Memgraph Ltd.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt; by using this file, you agree to be bound by the terms of the Business Source
// License, and you may not use this file except in compliance with the Business Source License.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
#include <iostream>
#include <mgp.hpp>
#include <optional>
#include <set>
#include <string>
#include <vector>
#include <nlohmann/json.hpp>
constexpr const std::string_view kFunctionStr2Object = "str2object";
constexpr const std::string_view kParameterString = "string";
// Forward declarations
std::optional<mgp::Value> ParseJsonToMgpValue(const nlohmann::json &json_obj, mgp_memory *memory);
mgp::Value ParseJsonToMgpMap(const nlohmann::json &json_obj, mgp_memory *memory) {
auto map = mgp::Map();
for (auto &[key, value] : json_obj.items()) {
auto sub_value = ParseJsonToMgpValue(value, memory).value();
map.Insert(key, sub_value);
}
return mgp::Value(std::move(map));
}
mgp::Value ParseJsonToMgpList(const nlohmann::json &json_array, mgp_memory *memory) {
auto list = mgp::List();
list.Reserve(json_array.size());
for (const auto &element : json_array) {
auto value = ParseJsonToMgpValue(element, memory).value();
list.AppendExtend(value);
}
return mgp::Value(std::move(list));
}
std::optional<mgp::Value> ParseJsonToMgpValue(const nlohmann::json &json_obj, mgp_memory *memory) {
if (json_obj.is_object()) {
return ParseJsonToMgpMap(json_obj, memory);
} else if (json_obj.is_array()) {
return ParseJsonToMgpList(json_obj, memory);
} else if (json_obj.is_string()) {
return mgp::Value(mgp::steal_type, mgp::value_make_string(json_obj.get<std::string>().c_str(), memory));
} else if (json_obj.is_number_integer()) {
return mgp::Value(mgp::steal_type, mgp::value_make_int(json_obj.get<int64_t>(), memory));
} else if (json_obj.is_number_float()) {
return mgp::Value(mgp::steal_type, mgp::value_make_double(json_obj.get<double>(), memory));
} else if (json_obj.is_boolean()) {
return mgp::Value(mgp::steal_type, mgp::value_make_bool(json_obj.get<bool>(), memory));
} else if (json_obj.is_null()) {
return mgp::Value();
} else {
return std::nullopt;
}
}
void str2object(mgp_list *args, mgp_func_context *ctx, mgp_func_result *res, mgp_memory *memory) {
try {
mgp::MemoryDispatcherGuard guard(memory);
// Retrieve the string argument
const auto string_arg = mgp::Value(mgp::ref_type, mgp::list_at(args, 0));
const auto json_object = nlohmann::json::parse(string_arg.ValueString());
std::optional<mgp::Value> maybe_result = ParseJsonToMgpValue(json_object, memory);
if (!maybe_result.has_value()) {
mgp::func_result_set_error_msg(
res,
"The end result is not one of the following JSON-language data types: object, array, "
"number, string, boolean, or null.",
memory);
return;
}
auto func_result = mgp::Result(res);
auto const &result = *maybe_result;
if (result.IsMap()) {
func_result.SetValue(result.ValueMap());
} else if (result.IsList()) {
func_result.SetValue(result.ValueList());
} else if (result.IsString()) {
func_result.SetValue(result.ValueString());
} else if (result.IsInt()) {
func_result.SetValue(result.ValueInt());
} else if (result.IsDouble()) {
func_result.SetValue(result.ValueDouble());
} else if (result.IsBool()) {
func_result.SetValue(result.ValueBool());
} else if (result.IsNull()) {
func_result.SetValue();
}
} catch (const std::exception &e) {
mgp::func_result_set_error_msg(res, e.what(), memory);
}
}
extern "C" int mgp_init_module(struct mgp_module *module, struct mgp_memory *memory) {
try {
mgp::MemoryDispatcherGuard guard(memory);
mgp::AddFunction(
str2object, kFunctionStr2Object, {mgp::Parameter(kParameterString, mgp::Type::String)}, module, memory);
} catch (const std::exception &e) {
std::cerr << "Error while initializing query module: " << e.what() << '\n';
return 1;
}
return 0;
}
extern "C" int mgp_shutdown_module() { return 0; }