Make JSON result sets produce nmeric valeus without quotes

Updated HTTPD protocol module to return application/json as the encoding
This commit is contained in:
Mark Riddoch
2015-02-20 10:09:01 +00:00
parent 8eb14235d1
commit 039d3c4c0b
2 changed files with 23 additions and 2 deletions

View File

@ -29,6 +29,7 @@
*/
#include <string.h>
#include <ctype.h>
#include <resultset.h>
#include <buffer.h>
#include <dcb.h>
@ -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");

View File

@ -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) {