[mw-devel] MW3 r1286 - trunk/src

arthur at sucs.org arthur at sucs.org
Fri Nov 9 17:10:02 GMT 2012


Author: arthur
Date: 2012-11-09 17:10:02 +0000 (Fri, 09 Nov 2012)
New Revision: 1286

Modified:
   trunk/src/socket.c
   trunk/src/socket.h
Log:
Add a few json helper functions


Modified: trunk/src/socket.c
===================================================================
--- trunk/src/socket.c	2012-11-09 16:49:18 UTC (rev 1285)
+++ trunk/src/socket.c	2012-11-09 17:10:02 UTC (rev 1286)
@@ -220,9 +220,11 @@
 /**** JSON handling functions ***/
 /* use http://www.digip.org/jansson/doc/2.3/  aka libjansson */
 
+/** read json object out of a message */
 json_t * ipcmsg_json_decode(ipc_message_t *msg)
 {
-	if (msg == NULL || msg->bodylen == 0) return NULL;
+	if (msg == NULL) return NULL;
+       	if (msg->bodylen == 0) return NULL;
 	json_error_t err;
 	json_t * js = json_loads(msg->body, 0, &err);
 
@@ -234,6 +236,7 @@
 	return js;
 }
 
+/** write json object into a message */
 int ipcmsg_json_encode(ipc_message_t *msg, json_t *js)
 {
 	if (msg == NULL) return -1;
@@ -252,3 +255,57 @@
 	msg->bodylen = strlen(newtext);
 	return 0;
 }
+
+/* always give as a workable top level json object */
+json_t * json_init(ipc_message_t * msg)
+{
+	json_t * js = NULL;
+	if (msg != NULL) {
+		js = ipcmsg_json_decode(msg);
+	}
+	if (js == NULL) js = json_object();
+	return js;
+}
+
+
+/** add/replace a string value in the base object */
+int json_addstring(json_t * js, const char * key, const char * value)
+{
+	json_t * tmp;
+
+	tmp = json_object_get(js, key);
+	if (tmp == NULL) {
+		tmp = json_string(value);
+		if (tmp == NULL) return -1;
+		json_object_set_new(js, key, tmp);
+	} else {
+		if (!json_is_string(tmp)) return -1;
+		json_string_set(tmp, value);
+	}
+	return 0;
+}
+
+int json_addint(json_t * js, const char * key, int value)
+{
+	json_t * tmp;
+
+	tmp = json_object_get(js, key);
+	if (tmp == NULL) {
+		tmp = json_integer(value);
+		if (tmp == NULL) return -1;
+		json_object_set_new(js, key, tmp);
+	} else {
+		if (!json_is_integer(tmp)) return -1;
+		json_integer_set(tmp, value);
+	}
+	return 0;
+}
+
+const char * json_getstring(json_t * js, const char * key)
+{
+	json_t * tmp;
+	tmp = json_object_get(js, key);
+	if (tmp == NULL) return NULL;
+	if (!json_is_string(tmp)) return NULL;
+	return json_string_value(tmp);
+}

Modified: trunk/src/socket.h
===================================================================
--- trunk/src/socket.h	2012-11-09 16:49:18 UTC (rev 1285)
+++ trunk/src/socket.h	2012-11-09 17:10:02 UTC (rev 1286)
@@ -3,6 +3,7 @@
 
 #include <unistd.h>
 #include <stdint.h>
+#include <jansson.h>
 #include "list.h"
 
 #define IPCPORT_DEFAULT	9999
@@ -58,5 +59,11 @@
 ipc_connection_t *ipcconn_create(void);
 int ipcconn_connect(const char *target);
 ipc_message_t *read_socket(ipc_connection_t *conn, int doread);
+json_t *ipcmsg_json_decode(ipc_message_t *msg);
+int ipcmsg_json_encode(ipc_message_t *msg, json_t *js);
+json_t *json_init(ipc_message_t *msg);
+int json_addstring(json_t *js, const char *key, const char *value);
+int json_addint(json_t *js, const char *key, int value);
+const char *json_getstring(json_t *js, const char *key);
 
 #endif




More information about the mw-devel mailing list