Merge pull request #3575 from tonhuisman/feature/add-setting-for-quoted-json-bool-values

[JSON] Add setting for unquoted JSON bool values
This commit is contained in:
TD-er
2021-04-20 12:43:49 +02:00
committed by GitHub
8 changed files with 32 additions and 3 deletions
+5
View File
@@ -190,6 +190,11 @@ WD I2C Address
The Watchdog timer can be accessed via I2C.
What can be read/set/changed must still be documented.
JSON bool output without quotes
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ESPEasy JSON output has always used quoted bool values, ``"true"`` and ``"false"``, that are in fact string values. According to JSON standards, bool values should be ``true`` and ``false``, so this setting selects what type of bool values will be emitted. As existing functionality is to be left unaltered/backward compatible as much as possible, by default this setting is unchecked.
Use SSDP
^^^^^^^^
+3
View File
@@ -216,6 +216,9 @@
#define DEFAULT_NETWORK_MEDIUM NetworkMedium_t::WIFI
#endif
#endif
#ifndef DEFAULT_JSON_BOOL_WITHOUT_QUOTES
#define DEFAULT_JSON_BOOL_WITHOUT_QUOTES false
#endif
+10
View File
@@ -170,6 +170,16 @@ void SettingsStruct_tmpl<N_TASKS>::PeriodicalScanWiFi(bool value) {
bitWrite(VariousBits1, 15, !value);
}
template<unsigned int N_TASKS>
bool SettingsStruct_tmpl<N_TASKS>::JSONBoolWithoutQuotes() const {
return bitRead(VariousBits1, 16);
}
template<unsigned int N_TASKS>
void SettingsStruct_tmpl<N_TASKS>::JSONBoolWithoutQuotes(bool value) {
bitWrite(VariousBits1, 16, value);
}
template<unsigned int N_TASKS>
bool SettingsStruct_tmpl<N_TASKS>::CombineTaskValues_SingleEvent(taskIndex_t taskIndex) const {
if (validTaskIndex(taskIndex)) {
+4
View File
@@ -106,6 +106,10 @@ class SettingsStruct_tmpl
bool PeriodicalScanWiFi() const;
void PeriodicalScanWiFi(bool value);
// When outputting JSON bools use quoted values (on, backward compatible) or use official JSON true/false unquoted
bool JSONBoolWithoutQuotes() const;
void JSONBoolWithoutQuotes(bool value);
// Flag indicating whether all task values should be sent in a single event or one event per task value (default behavior)
bool CombineTaskValues_SingleEvent(taskIndex_t taskIndex) const;
@@ -219,6 +219,8 @@ void ResetFactory()
*/
Settings.I2C_clockSpeed = DEFAULT_I2C_CLOCK_SPEED;
Settings.JSONBoolWithoutQuotes(DEFAULT_JSON_BOOL_WITHOUT_QUOTES);
#ifdef PLUGIN_DESCR
strcpy_P(Settings.Name, PSTR(PLUGIN_DESCR));
#endif // ifdef PLUGIN_DESCR
+2 -1
View File
@@ -393,6 +393,7 @@ String wrapIfContains(const String& value, char contains, char wrap) {
\*********************************************************************************************/
String to_json_object_value(const String& object, const String& value) {
String result;
bool isBool = (Settings.JSONBoolWithoutQuotes() && ((value.equalsIgnoreCase(F("true")) || value.equalsIgnoreCase(F("false")))));
result.reserve(object.length() + value.length() + 6);
wrap_String(object, F("\""), result);
@@ -403,7 +404,7 @@ String to_json_object_value(const String& object, const String& value) {
result += F("\"\"");
return result;
}
if (mustConsiderAsString(value)) {
if (!isBool && mustConsiderAsString(value)) {
// Is not a numerical value, or BIN/HEX notation, thus wrap with quotes
if ((value.indexOf('\n') != -1) || (value.indexOf('\r') != -1) || (value.indexOf('"') != -1)) {
// Must replace characters, so make a deepcopy
+3
View File
@@ -94,6 +94,7 @@ void handle_advanced() {
Settings.UseMaxTXpowerForSending(isFormItemChecked(getInternalLabel(LabelType::WIFI_SEND_AT_MAX_TX_PWR)));
Settings.NumberExtraWiFiScans = getFormItemInt(getInternalLabel(LabelType::WIFI_NR_EXTRA_SCANS));
Settings.PeriodicalScanWiFi(isFormItemChecked(getInternalLabel(LabelType::WIFI_PERIODICAL_SCAN)));
Settings.JSONBoolWithoutQuotes(isFormItemChecked(F("json_bool_with_quotes")));
addHtmlError(SaveSettings());
@@ -194,6 +195,8 @@ void handle_advanced() {
addFormCheckBox_disabled(F("Enable RTOS Multitasking"), F("usertosmultitasking"), Settings.UseRTOSMultitasking);
#endif // if defined(ESP32)
addFormCheckBox(F("JSON bool output without quotes"), F("json_bool_with_quotes"), Settings.JSONBoolWithoutQuotes());
#ifdef USES_SSDP
addFormCheckBox_disabled(F("Use SSDP"), F("usessdp"), Settings.UseSSDP);
#endif // ifdef USES_SSDP
+3 -2
View File
@@ -523,9 +523,10 @@ void handle_buildinfo() {
\*********************************************************************************************/
void stream_to_json_value(const String& value) {
NumericalType detectedType;
bool isNum = isNumerical(value, detectedType);
bool isNum = isNumerical(value, detectedType);
bool isBool = (Settings.JSONBoolWithoutQuotes() && ((value.equalsIgnoreCase(F("true")) || value.equalsIgnoreCase(F("false")))));
if ((value.length() == 0) || !isNum || mustConsiderAsString(detectedType)) {
if (!isBool && ((value.length() == 0) || !isNum || mustConsiderAsString(detectedType))) {
// Either empty, not a numerical or a BIN/HEX notation.
String html;
html.reserve(value.length() + 2);