From 107d899178e71b239cf725ecc1a2e4ed6f1ef120 Mon Sep 17 00:00:00 2001 From: Kinsey Moore Date: Sat, 24 Feb 2018 01:08:46 -0600 Subject: [PATCH 1/4] Send single quote as single char Don't send a single quote as a string. This could cause extra processing in downstream clients. --- aJSON.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aJSON.cpp b/aJSON.cpp index 1bb9320..b1e368e 100644 --- a/aJSON.cpp +++ b/aJSON.cpp @@ -432,7 +432,7 @@ aJsonStream::parseString(aJsonObject *item) int aJsonStream::printStringPtr(const char *str) { - this->print("\""); + this->print('"'); char* ptr = (char*) str; if (ptr != NULL) { From 23d887cbe3ce066ac0405e848a9e5b4a55149360 Mon Sep 17 00:00:00 2001 From: Kinsey Moore Date: Sat, 24 Feb 2018 01:09:53 -0600 Subject: [PATCH 2/4] Short circuit empty strings This allows faster processing of empty strings ("") and doesn't burden downstream clients with processing "". --- aJSON.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aJSON.cpp b/aJSON.cpp index b1e368e..9df9e0e 100644 --- a/aJSON.cpp +++ b/aJSON.cpp @@ -434,7 +434,7 @@ aJsonStream::printStringPtr(const char *str) { this->print('"'); char* ptr = (char*) str; - if (ptr != NULL) + if (ptr != NULL && *ptr != '\0') { while (*ptr != 0) { From 7476c5d1104000eadc79f6eca7353a40c0f8fadd Mon Sep 17 00:00:00 2001 From: Kinsey Moore Date: Sat, 24 Feb 2018 01:14:21 -0600 Subject: [PATCH 3/4] Implement write(const uint8_t*,size_t) passthrough This allows strings to be passed as is instead of being decomposed into individual characters. This is more efficient for some clients and those that don't implement it can fall back on the default print implementation. --- aJSON.cpp | 6 ++++++ aJSON.h | 1 + 2 files changed, 7 insertions(+) diff --git a/aJSON.cpp b/aJSON.cpp index 9df9e0e..c5c47a0 100644 --- a/aJSON.cpp +++ b/aJSON.cpp @@ -102,6 +102,12 @@ aJsonStream::write(uint8_t ch) return stream()->write(ch); } +size_t +aJsonStream::write(const uint8_t *str, size_t len) +{ + return stream()->write(str, len); +} + size_t aJsonStream::readBytes(uint8_t *buffer, size_t len) { diff --git a/aJSON.h b/aJSON.h index a409a92..509d91f 100644 --- a/aJSON.h +++ b/aJSON.h @@ -114,6 +114,7 @@ class aJsonStream : public Print { /* Inherited from class Print. */ virtual size_t write(uint8_t ch); + virtual size_t write(const uint8_t *str, size_t len); /* stream attribute is used only from virtual functions, * therefore an object inheriting aJsonStream may avoid From acfd9cab8a185aaba450997a898d16988f36031d Mon Sep 17 00:00:00 2001 From: Kinsey Moore Date: Sat, 24 Feb 2018 01:16:04 -0600 Subject: [PATCH 4/4] Don't decompose strings that have no escapes This pushes strings without characters that need to be escaped directly to the Print implementation instead of decomposing them anyway. This can greatly improve performance for clients with significant per-call overhead. --- aJSON.cpp | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/aJSON.cpp b/aJSON.cpp index c5c47a0..17baa10 100644 --- a/aJSON.cpp +++ b/aJSON.cpp @@ -442,6 +442,26 @@ aJsonStream::printStringPtr(const char *str) char* ptr = (char*) str; if (ptr != NULL && *ptr != '\0') { + char* check = ptr; + bool hasEscapes = false; + while (*check != 0) + { + if ((unsigned char) *check > 31 && *check != '\"' && *check != '\\') + { + } + else + { + hasEscapes = true; + break; + } + check++; + } + if (!hasEscapes) + { + this->print(ptr); + this->print('\"'); + return 0; + } while (*ptr != 0) { if ((unsigned char) *ptr > 31 && *ptr != '\"' && *ptr != '\\')