Use switch instead of if else tree in jsonFromKvInternal().
This is more efficient but more importantly it is easier to read.
@@ -767,14 +767,18 @@ jsonFromKvInternal(const KeyValue *kv, String *indentSpace, String *indentDepth)
// NULL value
if (value == NULL)
strCat(result, "null");
- // KeyValue
- else if (varType(value) == varTypeKeyValue)
+ else
+ {
+ switch (varType(value))
+ {
+ case varTypeKeyValue:
{
strCat(indentDepth, strPtr(indentSpace));
strCat(result, strPtr(jsonFromKvInternal(kvDup(varKv(value)), indentSpace, indentDepth)));
+ break;
}
- // VariantList
- else if (varType(value) == varTypeVariantList)
+
+ case varTypeVariantList:
{
// If the array is empty, then do not add formatting, else process the array.
if (varVarLst(value) == NULL)
@@ -824,16 +828,26 @@ jsonFromKvInternal(const KeyValue *kv, String *indentSpace, String *indentDepth)
strCatFmt(result, "%s]", strPtr(indentDepth));
}
+
+ break;
}
+
// String
- else if (varType(value) == varTypeString)
+ case varTypeString:
{
jsonFromStrInternal(result, varStr(value));
+ break;
}
- // Numeric, Boolean or other type
- else
+
+ default:
+ {
strCat(result, strPtr(varStrForce(value)));
+ break;
}
+ }
+ }
+ }
+
if (strSize(indentDepth) > strSize(indentSpace))
strTrunc(indentDepth, (int)(strSize(indentDepth) - strSize(indentSpace)));
