From fcb4ed5e2cbba4e7e395d32759314fd3c0d2409e Mon Sep 17 00:00:00 2001 From: Nicolas Favre-Felix Date: Tue, 21 Dec 2010 19:49:20 +0100 Subject: [PATCH] JSON fixes. --- README.markdown | 31 ++++++++++++++++++++++++------- json.c | 11 +++++------ 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/README.markdown b/README.markdown index 7a638d7..d7ad364 100644 --- a/README.markdown +++ b/README.markdown @@ -1,6 +1,6 @@ # About -A very simple prototype providing an HTTP interface to Redis. +A very simple prototype providing an HTTP interface to Redis. It uses [hiredis](https://github.com/antirez/hiredis) and [jansson](https://github.com/akheron/jansson).
 make clean all
@@ -19,12 +19,12 @@ curl -d "GET/hello" http://127.0.0.1:7379/
 * Add meta-data info per key (MIME type in a second key, for instance)
 * Find a way to format multi-bulk data
 * Support PUT, DELETE, HEAD?
-* Add JSON output
 * Add JSONP callbacks
 * Add support for Redis UNIX socket
 * Enrich config file
 	* Provide timeout
 	* Restrict commands by IP range
+* Add your own here or send your suggestions on twitter [@yowgi](http://twitter.com/yowgi).
 
 # HTTP error codes
 * Missing key: 404 Not Found
@@ -34,10 +34,27 @@ curl -d "GET/hello" http://127.0.0.1:7379/
 
 # JSON output
 
-## /GET/x
-`{"GET": "value here"}`
+The URI `/COMMAND/arg0/arg1/.../argN` returns a JSON object with the command as a key and the result as a value.
 
-## /INCR/y
-`{"INCR": 42}`
+**Examples:**
+
+// string
+$ curl  http://127.0.0.1:7379/GET/y
+{"GET":"42"}
+
+// number
+$ curl  http://127.0.0.1:7379/INCR/y
+{"INCR":42}
+
+// list
+$ curl  http://127.0.0.1:7379/LRANGE/x/0/1
+{"LRANGE":["abc","def"]}
 
-## 
+// status
+$ curl  http://127.0.0.1:7379/TYPE/y
+{"TYPE":[true,"string"]}
+
+// error, which is basically a status
+$ curl  http://127.0.0.1:7379/MAKE-ME-COFFEE
+{"MAKE-ME-COFFEE":[false,"ERR unknown command 'MAKE-ME-COFFEE'"]}
+
diff --git a/json.c b/json.c index 9bcfc39..4fbb14d 100644 --- a/json.c +++ b/json.c @@ -61,20 +61,19 @@ json_encode(const struct cmd *cmd, const redisReply *r) { switch(r->type) { case REDIS_REPLY_STATUS: case REDIS_REPLY_ERROR: - jlist = json_array(); json_array_append_new(jlist, r->type == REDIS_REPLY_ERROR ? json_false() : json_true()); json_array_append_new(jlist, json_string(r->str)); - json_object_set(jroot, verb, jlist); + json_object_set_new(jroot, verb, jlist); break; case REDIS_REPLY_STRING: - json_object_set(jroot, verb, json_string(r->str)); + json_object_set_new(jroot, verb, json_string(r->str)); break; case REDIS_REPLY_INTEGER: - json_object_set(jroot, verb, json_integer(r->integer)); + json_object_set_new(jroot, verb, json_integer(r->integer)); break; case REDIS_REPLY_ARRAY: @@ -93,11 +92,11 @@ json_encode(const struct cmd *cmd, const redisReply *r) { break; } } - json_object_set(jroot, verb, jlist); + json_object_set_new(jroot, verb, jlist); break; default: - json_object_set(jroot, verb, json_null()); + json_object_set_new(jroot, verb, json_null()); break; }