Skip to content

Commit

Permalink
Use double quotes in JSON for non-numeric values (#929)
Browse files Browse the repository at this point in the history
  • Loading branch information
xoseperez committed Jun 12, 2018
1 parent 57bacc7 commit f651f5f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
6 changes: 5 additions & 1 deletion code/espurna/api.ino
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,11 @@ ArRequestHandlerFunction _bindAPI(unsigned int apiID) {
// Format response according to the Accept header
if (_asJson(request)) {
char buffer[64];
snprintf_P(buffer, sizeof(buffer), PSTR("{ \"%s\": %s }"), api.key, value);
if (isNumber(value)) {
snprintf_P(buffer, sizeof(buffer), PSTR("{ \"%s\": %s }"), api.key, value);
} else {
snprintf_P(buffer, sizeof(buffer), PSTR("{ \"%s\": \"%s\" }"), api.key, value);
}
request->send(200, "application/json", buffer);
} else {
request->send(200, "text/plain", value);
Expand Down
16 changes: 16 additions & 0 deletions code/espurna/utils.ino
Original file line number Diff line number Diff line change
Expand Up @@ -589,3 +589,19 @@ void nice_delay(unsigned long ms) {
int __get_adc_mode() {
return (int) (ADC_MODE_VALUE);
}

bool isNumber(const char * s) {
unsigned char len = strlen(s);
bool decimal = false;
for (unsigned char i=0; i<len; i++) {
if (s[i] == '-') {
if (i>0) return false;
} else if (s[i] == '.') {
if (decimal) return false;
decimal = true;
} else if (!isdigit(s[i])) {
return false;
}
}
return true;
}

0 comments on commit f651f5f

Please sign in to comment.