SWITCH_DECLARE(const char *)Event::serialize(const char *format)
{
	this_check("");


	switch_safe_free(serialized_string);
	
	if (!event) {
		return "";
	}

	if (format && !strcasecmp(format, "xml")) {
		switch_xml_t xml;
		if ((xml = switch_event_xmlize(event, SWITCH_VA_NONE))) {
			serialized_string = switch_xml_toxml(xml, SWITCH_FALSE);
			switch_xml_free(xml);
			return serialized_string;
		} else {
			return "";
		}
	} else if (format && !strcasecmp(format, "json")) {
		switch_event_serialize_json(event, &serialized_string);
		return serialized_string;
	} else {
		if (switch_event_serialize(event, &serialized_string, SWITCH_TRUE) == SWITCH_STATUS_SUCCESS) {
			char *new_serialized_string = switch_mprintf("'%s'", serialized_string);
			free(serialized_string);
			serialized_string = new_serialized_string;
			return serialized_string;
		}
	}
	
	return "";

}
Example #2
0
void event_handler(switch_event_t *event) {
	char *json;
	wsh_t *wsh = (TSession *)event->bind_user_data;
	switch_event_serialize_json(event, &json);
	ws_write_frame(wsh, WSOC_TEXT, json, strlen(json));
	free(json);
}