#include <pthread.h>
#include <json-c/json.h>
+#if !defined(JSON_C_TO_STRING_NOSLASHESCAPE)
+#define JSON_C_TO_STRING_NOSLASHESCAPE 0
+#endif
#include "afb-ws.h"
#include "afb-wsj1.h"
struct json_object *afb_wsj1_msg_object_j(struct afb_wsj1_msg *msg)
{
+ enum json_tokener_error jerr;
struct json_object *object = msg->object_j;
if (object == NULL) {
pthread_mutex_lock(&msg->wsj1->mutex);
json_tokener_reset(msg->wsj1->tokener);
- object = json_tokener_parse_ex(msg->wsj1->tokener, msg->object_s, (int)msg->object_s_length);
+ object = json_tokener_parse_ex(msg->wsj1->tokener, msg->object_s, 1 + (int)msg->object_s_length);
+ jerr = json_tokener_get_error(msg->wsj1->tokener);
pthread_mutex_unlock(&msg->wsj1->mutex);
- if (object == NULL) {
+ if (jerr != json_tokener_success) {
/* lazy error detection of json request. Is it to improve? */
object = json_object_new_string_len(msg->object_s, (int)msg->object_s_length);
}
int afb_wsj1_send_event_j(struct afb_wsj1 *wsj1, const char *event, struct json_object *object)
{
- const char *objstr = json_object_to_json_string_ext(object, JSON_C_TO_STRING_PLAIN);
+ const char *objstr = json_object_to_json_string_ext(object, JSON_C_TO_STRING_PLAIN|JSON_C_TO_STRING_NOSLASHESCAPE);
int rc = afb_wsj1_send_event_s(wsj1, event, objstr);
json_object_put(object);
return rc;
int afb_wsj1_call_j(struct afb_wsj1 *wsj1, const char *api, const char *verb, struct json_object *object, void (*on_reply)(void *closure, struct afb_wsj1_msg *msg), void *closure)
{
- const char *objstr = json_object_to_json_string_ext(object, JSON_C_TO_STRING_PLAIN);
+ const char *objstr = json_object_to_json_string_ext(object, JSON_C_TO_STRING_PLAIN|JSON_C_TO_STRING_NOSLASHESCAPE);
int rc = afb_wsj1_call_s(wsj1, api, verb, objstr, on_reply, closure);
json_object_put(object);
return rc;
int afb_wsj1_reply_j(struct afb_wsj1_msg *msg, struct json_object *object, const char *token, int iserror)
{
- const char *objstr = json_object_to_json_string_ext(object, JSON_C_TO_STRING_PLAIN);
+ const char *objstr = json_object_to_json_string_ext(object, JSON_C_TO_STRING_PLAIN|JSON_C_TO_STRING_NOSLASHESCAPE);
int rc = afb_wsj1_reply_s(msg, objstr, token, iserror);
json_object_put(object);
return rc;