generated from obsproject/obs-plugintemplate
-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
jsonpath.cpp
61 lines (56 loc) · 1.72 KB
/
jsonpath.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include "request-data.h"
#include "errors.h"
#include <jsoncons/basic_json.hpp>
#include <jsoncons/json_parser.hpp>
#include <jsoncons_ext/jsonpath/jsonpath.hpp>
#include <obs-module.h>
struct request_data_handler_response parse_json(struct request_data_handler_response response,
const url_source_request_data *request_data)
{
UNUSED_PARAMETER(request_data);
// Parse the response as JSON
jsoncons::json json;
try {
json = jsoncons::json::parse(response.body);
} catch (jsoncons::json_exception &e) {
return make_fail_parse_response(e.what());
}
// Return the whole JSON object
response.body_parts_parsed.push_back(json.as_string());
return response;
}
struct request_data_handler_response parse_json_path(struct request_data_handler_response response,
const url_source_request_data *request_data)
{
// Parse the response as JSON
jsoncons::json json;
try {
json = jsoncons::json::parse(response.body);
} catch (jsoncons::json_exception &e) {
return make_fail_parse_response(e.what());
}
std::vector<std::string> parsed_output = {};
// Get the output value
if (request_data->output_json_path != "") {
try {
const auto value = jsoncons::jsonpath::json_query(
json, request_data->output_json_path);
if (value.is_array()) {
// extract array items as strings
for (const auto &item : value.array_range()) {
parsed_output.push_back(item.as_string());
}
} else {
parsed_output.push_back(value.as_string());
}
} catch (jsoncons::json_exception &e) {
return make_fail_parse_response(e.what());
}
} else {
// Return the whole JSON object
parsed_output.clear();
parsed_output.push_back(json.as_string());
}
response.body_parts_parsed = parsed_output;
return response;
}