From 039d3c4c0bcb3c1b1f51489216cf56e627ba99ce Mon Sep 17 00:00:00 2001 From: Mark Riddoch Date: Fri, 20 Feb 2015 10:09:01 +0000 Subject: [PATCH] Make JSON result sets produce nmeric valeus without quotes Updated HTTPD protocol module to return application/json as the encoding --- server/core/resultset.c | 23 ++++++++++++++++++++++- server/modules/protocol/httpd.c | 2 +- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/server/core/resultset.c b/server/core/resultset.c index fbb42ee0b..8c915bfa1 100644 --- a/server/core/resultset.c +++ b/server/core/resultset.c @@ -29,6 +29,7 @@ */ #include +#include #include #include #include @@ -402,6 +403,24 @@ uint8_t *ptr; return dcb->func.write(dcb, pkt); } +/** + * Return true if the string only contains numerics + * + * @param value String to test + * @return Non-zero if the string is made of of numeric values + */ +static int +value_is_numeric(char *value) +{ + while (*value) + { + if (!isdigit(*value)) + return 0; + value++; + } + return 1; +} + /** * Stream a result set encoding it as a JSON object * Each row is retrieved by calling the function passed in the @@ -430,7 +449,9 @@ int rowno = 0; { dcb_printf(dcb, "\"%s\" : ", col->name); - if (row->cols[i]) + if (row->cols[i] && value_is_numeric(row->cols[i])) + dcb_printf(dcb, "%s", row->cols[i]); + else if (row->cols[i]) dcb_printf(dcb, "\"%s\"", row->cols[i]); else dcb_printf(dcb, "NULL"); diff --git a/server/modules/protocol/httpd.c b/server/modules/protocol/httpd.c index 51ba707a9..0de934dec 100644 --- a/server/modules/protocol/httpd.c +++ b/server/modules/protocol/httpd.c @@ -501,7 +501,7 @@ static void httpd_send_headers(DCB *dcb, int final) strftime(date, sizeof(date), fmt, localtime(&httpd_current_time)); - dcb_printf(dcb, "HTTP/1.1 200 OK\r\nDate: %s\r\nServer: %s\r\nConnection: close\r\nContent-Type: text/plain\r\n", date, HTTP_SERVER_STRING); + dcb_printf(dcb, "HTTP/1.1 200 OK\r\nDate: %s\r\nServer: %s\r\nConnection: close\r\nContent-Type: application/json\r\n", date, HTTP_SERVER_STRING); /* close the headers */ if (final) {