Browse Source

Use double quotes in JSON for non-numeric values (#929)

fastled^2
Xose Pérez 6 years ago
parent
commit
f651f5f18d
2 changed files with 21 additions and 1 deletions
  1. +5
    -1
      code/espurna/api.ino
  2. +16
    -0
      code/espurna/utils.ino

+ 5
- 1
code/espurna/api.ino View File

@ -102,7 +102,11 @@ ArRequestHandlerFunction _bindAPI(unsigned int apiID) {
// Format response according to the Accept header // Format response according to the Accept header
if (_asJson(request)) { if (_asJson(request)) {
char buffer[64]; 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); request->send(200, "application/json", buffer);
} else { } else {
request->send(200, "text/plain", value); request->send(200, "text/plain", value);


+ 16
- 0
code/espurna/utils.ino View File

@ -589,3 +589,19 @@ void nice_delay(unsigned long ms) {
int __get_adc_mode() { int __get_adc_mode() {
return (int) (ADC_MODE_VALUE); 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;
}

Loading…
Cancel
Save