From 7c99d4d63d4641e45e17c25dd1ef3a521658fbda Mon Sep 17 00:00:00 2001 From: Benoit Blanchon Date: Fri, 1 Aug 2014 15:22:30 +0200 Subject: [PATCH] Reduced usages of EscapedString --- JsonGenerator/EscapedString.h | 12 +++--------- JsonGenerator/JsonObjectBase.cpp | 7 ++++--- JsonGenerator/JsonObjectBase.h | 4 ++-- JsonGenerator/JsonValue.cpp | 3 ++- JsonGenerator/JsonValue.h | 12 ++++++------ JsonGeneratorTests/EscapedStringTests.cpp | 7 +++---- 6 files changed, 20 insertions(+), 25 deletions(-) diff --git a/JsonGenerator/EscapedString.h b/JsonGenerator/EscapedString.h index 5975d9ad..c9ef776f 100644 --- a/JsonGenerator/EscapedString.h +++ b/JsonGenerator/EscapedString.h @@ -6,7 +6,6 @@ #pragma once #include "Print.h" -#include // for strcmp namespace ArduinoJson { @@ -16,18 +15,13 @@ namespace ArduinoJson { public: - void set(const char* s) - { - rawString = s; + EscapedString(const char* s) + : rawString(s) + { } size_t printTo(Print&) const; - bool equals(char const* s) - { - return strcmp(s, rawString) == 0; - } - private: const char* rawString; }; diff --git a/JsonGenerator/JsonObjectBase.cpp b/JsonGenerator/JsonObjectBase.cpp index dc708ae7..03c21add 100644 --- a/JsonGenerator/JsonObjectBase.cpp +++ b/JsonGenerator/JsonObjectBase.cpp @@ -4,6 +4,7 @@ */ #include "JsonObjectBase.h" +#include // for strcmp using namespace ArduinoJson::Generator; using namespace ArduinoJson::Internals; @@ -19,7 +20,7 @@ size_t JsonObjectBase::printTo(Print& p) const const KeyValuePair* current = items; for (int i = count; i > 0; i--) { - n += current->key.printTo(p); + n += EscapedString(current->key).printTo(p); n += p.write(':'); n += current->value.printTo(p); @@ -40,7 +41,7 @@ JsonValue& JsonObjectBase::operator[](char const* key) { for (int i = 0; i < count; ++i) { - if (items[i].key.equals(key)) + if (!strcmp(items[i].key, key)) { return items[i].value; } @@ -49,6 +50,6 @@ JsonValue& JsonObjectBase::operator[](char const* key) if (count >= capacity) return JsonValue::null(); - items[count].key.set(key); + items[count].key = key; return items[count++].value; } \ No newline at end of file diff --git a/JsonGenerator/JsonObjectBase.h b/JsonGenerator/JsonObjectBase.h index e8e13188..26f59fee 100644 --- a/JsonGenerator/JsonObjectBase.h +++ b/JsonGenerator/JsonObjectBase.h @@ -38,8 +38,8 @@ namespace ArduinoJson struct KeyValuePair { - Internals::EscapedString key; - JsonValue value; + const char* key; + JsonValue value; }; JsonObjectBase(KeyValuePair* items, int capacity) diff --git a/JsonGenerator/JsonValue.cpp b/JsonGenerator/JsonValue.cpp index 8c444b00..5ce603da 100644 --- a/JsonGenerator/JsonValue.cpp +++ b/JsonGenerator/JsonValue.cpp @@ -7,6 +7,7 @@ #include "JsonValue.h" using namespace ArduinoJson::Generator; +using namespace ArduinoJson::Internals; JsonValue JsonValue::nullInstance; @@ -30,5 +31,5 @@ size_t JsonValue::printPrintableTo(const Content& c, Print& p) size_t JsonValue::printStringTo(const Content& c, Print& p) { - return c.asString.printTo(p); + return EscapedString(c.asString).printTo(p); } \ No newline at end of file diff --git a/JsonGenerator/JsonValue.h b/JsonGenerator/JsonValue.h index d7202a6b..f45cbb10 100644 --- a/JsonGenerator/JsonValue.h +++ b/JsonGenerator/JsonValue.h @@ -44,7 +44,7 @@ namespace ArduinoJson void operator=(const char* value) { printToImpl = &printStringTo; - content.asString.set(value); + content.asString = value; } void operator=(double value) @@ -108,11 +108,11 @@ namespace ArduinoJson private: union Content { - bool asBool; - long asLong; - Printable* asPrintable; - Internals::EscapedString asString; - double asDouble; + bool asBool; + double asDouble; + long asLong; + Printable* asPrintable; + const char* asString; }; Content content; diff --git a/JsonGeneratorTests/EscapedStringTests.cpp b/JsonGeneratorTests/EscapedStringTests.cpp index b065c2f0..6b72ecc9 100644 --- a/JsonGeneratorTests/EscapedStringTests.cpp +++ b/JsonGeneratorTests/EscapedStringTests.cpp @@ -16,8 +16,7 @@ namespace JsonGeneratorTests { char buffer[1024]; size_t returnValue; - EscapedString escapedString; - + public: TEST_METHOD(Null) @@ -83,8 +82,8 @@ namespace JsonGeneratorTests private: void whenInputIs(const char* input) { - StringBuilder sb(buffer, sizeof(buffer)); - escapedString.set(input); + StringBuilder sb(buffer, sizeof(buffer)); + EscapedString escapedString = input; returnValue = escapedString.printTo(sb); }