forked from SmingHub/Sming
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add string escaping to JSON formatter (SmingHub#2875)
JSON does not support multiline text so control characters must be escaped. This PR adds a generic `escapeControls` function to the `Formatter` classes, which is used by `Format::json::escape()`.
- Loading branch information
Showing
5 changed files
with
137 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/**** | ||
* Sming Framework Project - Open Source framework for high efficiency native ESP8266 development. | ||
* Created 2015 by Skurydin Alexey | ||
* http://github.com/SmingHub/Sming | ||
* All files of the Sming Core are provided under the LGPL v3 license. | ||
* | ||
* Formatter.cpp | ||
* | ||
* @author mikee47 <mike@sillyhouse.net> Aug 2024 | ||
* | ||
****/ | ||
|
||
#include "Formatter.h" | ||
|
||
namespace | ||
{ | ||
/** | ||
* @brief Get character used for standard escapes | ||
* @param c Code to be escaped | ||
* @retval char Corresponding character, NUL if there isn't a standard escape | ||
*/ | ||
char escapeChar(char c) | ||
{ | ||
switch(c) { | ||
case '\0': | ||
return '0'; | ||
case '\'': | ||
return '\''; | ||
case '\"': | ||
return '"'; | ||
case '\?': | ||
return '?'; | ||
case '\\': | ||
return '\\'; | ||
case '\a': | ||
return 'a'; | ||
case '\b': | ||
return 'b'; | ||
case '\f': | ||
return 'f'; | ||
case '\n': | ||
return 'n'; | ||
case '\r': | ||
return 'r'; | ||
case '\t': | ||
return 't'; | ||
case '\v': | ||
return 'v'; | ||
default: | ||
return '\0'; | ||
} | ||
} | ||
|
||
} // namespace | ||
|
||
namespace Format | ||
{ | ||
unsigned escapeControls(String& value) | ||
{ | ||
// Count number of extra characters we'll need to insert | ||
unsigned extra{0}; | ||
for(auto& c : value) { | ||
if(escapeChar(c)) { | ||
extra += 1; // "\" | ||
} else if(uint8_t(c) < 0x20) { | ||
extra += 3; // "\xnn" | ||
} | ||
} | ||
if(extra == 0) { | ||
return 0; | ||
} | ||
auto len = value.length(); | ||
if(!value.setLength(len + extra)) { | ||
return 0; | ||
} | ||
char* out = value.begin(); | ||
const char* in = out; | ||
memmove(out + extra, in, len); | ||
in += extra; | ||
while(len--) { | ||
uint8_t c = *in++; | ||
auto esc = escapeChar(c); | ||
if(esc) { | ||
*out++ = '\\'; | ||
*out++ = esc; | ||
} else if(c < 0x20) { | ||
*out++ = '\\'; | ||
*out++ = 'x'; | ||
*out++ = hexchar(uint8_t(c) >> 4); | ||
*out++ = hexchar(uint8_t(c) & 0x0f); | ||
} else { | ||
*out++ = c; | ||
} | ||
} | ||
return extra; | ||
} | ||
|
||
} // namespace Format |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#include <HostTests.h> | ||
#include <Data/Format/Json.h> | ||
|
||
class FormatterTest : public TestGroup | ||
{ | ||
public: | ||
FormatterTest() : TestGroup(_F("Formatter")) | ||
{ | ||
} | ||
|
||
void execute() override | ||
{ | ||
DEFINE_FSTR_LOCAL(text1, "A JSON\ntest string\twith escapes\x12\0\n" | ||
"Worth maybe \xc2\xa3" | ||
"0.53.") | ||
DEFINE_FSTR_LOCAL(text1b, "A JSON\\ntest string\\twith escapes\\x12\\0\\n" | ||
"Worth maybe \xc2\xa3" | ||
"0.53.") | ||
|
||
Serial << text1 << endl; | ||
String s(text1); | ||
Format::json.escape(s); | ||
REQUIRE_EQ(s, text1b); | ||
} | ||
}; | ||
|
||
void REGISTER_TEST(Formatter) | ||
{ | ||
registerGroup<FormatterTest>(); | ||
} |