diff --git a/src/mod/languages/mod_v8/mod_v8.cpp b/src/mod/languages/mod_v8/mod_v8.cpp index 2f37a8cd0e..2e4bdc3b33 100644 --- a/src/mod/languages/mod_v8/mod_v8.cpp +++ b/src/mod/languages/mod_v8/mod_v8.cpp @@ -712,6 +712,45 @@ SWITCH_STANDARD_API(jsapi_function) return SWITCH_STATUS_SUCCESS; } + +SWITCH_STANDARD_JSON_API(json_function) +{ + char *json_text = NULL; + cJSON *path = NULL, *data = NULL; + switch_stream_handle_t stream = { 0 }; + + if ((data = cJSON_GetObjectItem(json, "data"))) { + path = cJSON_GetObjectItem(data, "path"); + } + + if (!(path && data)) { + goto end; + } + + SWITCH_STANDARD_STREAM(stream); + + json_text = cJSON_PrintUnformatted(data); + switch_event_create(&stream.param_event, SWITCH_EVENT_REQUEST_PARAMS); + switch_event_add_header_string(stream.param_event, SWITCH_STACK_BOTTOM, "JSON", json_text); + switch_safe_free(json_text); + + v8_parse_and_execute(session, (char *) path->valuestring, &stream, NULL); + + *json_reply = cJSON_Parse((char *)stream.data); + + end: + + if (!*json_reply) { + *json_reply = cJSON_CreateObject(); + cJSON_AddItemToObject(*json_reply, "error", cJSON_CreateString("parse error in return val or invalid data supplied")); + } + + switch_event_destroy(&stream.param_event); + switch_safe_free(stream.data); + + return SWITCH_STATUS_SUCCESS; +} + SWITCH_STANDARD_API(launch_async) { if (zstr(cmd)) { @@ -728,6 +767,7 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_v8_load) { switch_application_interface_t *app_interface; switch_chat_application_interface_t *chat_app_interface; + switch_json_api_interface_t *json_api_interface; if (load_modules() != SWITCH_STATUS_SUCCESS) { return SWITCH_STATUS_FALSE; @@ -754,6 +794,8 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_v8_load) SWITCH_ADD_APP(app_interface, "javascript", "Launch JS ivr", "Run a javascript ivr on a channel", v8_dp_function, "