-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathwrapper.cpp
More file actions
51 lines (41 loc) · 1.56 KB
/
wrapper.cpp
File metadata and controls
51 lines (41 loc) · 1.56 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
#include <string>
#include <stdexcept>
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include "jsonata-es5.h"
#include "duktape.h"
static void my_duk_abort(void * udata, const char *msg) {
abort();
}
class DukContext {
duk_context *ctx;
public:
DukContext():ctx(duk_create_heap(NULL, NULL, NULL, NULL, my_duk_abort)) {
if (duk_peval_string(ctx, JAVASCRIP_JSONATA_LIB.c_str()) != 0) {
throw std::domain_error("Unable to load JSONATA into duktape JavaScript engine");
}
};
~DukContext() {
duk_destroy_heap(ctx);
};
std::string jsonata_call(std::string xform, std::string json_data) {
std::string command = std::string("JSON.stringify(jsonata('") + xform + std::string("').evaluate(") + json_data + std::string("));");
if (duk_peval_string(ctx,command.c_str()) != 0) {
duk_to_object(ctx,-1);
throw pybind11::value_error(duk_json_encode(ctx,-1));//std::string("Error:") +
}
return duk_safe_to_string(ctx, -1);
}
};
std::string jsonata_wrapper_cpp(std::string xform, std::string json_data) {
DukContext myctx;
return myctx.jsonata_call(xform, json_data);
}
PYBIND11_MODULE(_jsonata, m) {
m.doc() = "Python Wrapper for JDONata JavaScript library";
m.def("transform", &jsonata_wrapper_cpp, "Apply JSONata transform to JSON data and returnt the result.",
pybind11::arg("xform"), pybind11::arg("json_data"));
pybind11::class_<DukContext>(m, "Context")
.def(pybind11::init<>())
.def("__call__", &DukContext::jsonata_call);
}