Removed JsonValue::null(), moved the instance to JsonObjectBase

This commit is contained in:
Benoit Blanchon
2014-08-02 15:55:46 +02:00
parent 65e8b6d405
commit 5cc06180e6
5 changed files with 33 additions and 15 deletions

View File

@ -9,6 +9,10 @@
using namespace ArduinoJson::Generator; using namespace ArduinoJson::Generator;
using namespace ArduinoJson::Internals; using namespace ArduinoJson::Internals;
JsonValue JsonObjectBase::nullValue;
size_t JsonObjectBase::printTo(Print& p) const size_t JsonObjectBase::printTo(Print& p) const
{ {
size_t n = 0; size_t n = 0;
@ -51,10 +55,19 @@ JsonValue& JsonObjectBase::operator[](char const* key)
p++; p++;
} }
if (count >= capacity) JsonValue* value;
return JsonValue::null();
count++; if (count < capacity)
p->key = key; {
return p->value; count++;
p->key = key;
value = &p->value;
}
else
{
value = &nullValue;
}
value->reset();
return *value;
} }

View File

@ -50,6 +50,8 @@ namespace ArduinoJson
private: private:
KeyValuePair* items; KeyValuePair* items;
int capacity, count; int capacity, count;
static JsonValue nullValue;
}; };
} }
} }

View File

@ -9,8 +9,6 @@
using namespace ArduinoJson::Generator; using namespace ArduinoJson::Generator;
using namespace ArduinoJson::Internals; using namespace ArduinoJson::Internals;
JsonValue JsonValue::nullInstance;
size_t JsonValue::printBoolTo(const Content& c, Print& p) size_t JsonValue::printBoolTo(const Content& c, Print& p)
{ {
return p.print(c.asBool ? "true" : "false"); return p.print(c.asBool ? "true" : "false");

View File

@ -100,9 +100,10 @@ namespace ArduinoJson
return printToImpl(content, p); return printToImpl(content, p);
} }
static JsonValue& null() void reset()
{ {
return nullInstance; content.asDouble = 0;
printToImpl = printStringTo;
} }
private: private:
@ -129,8 +130,6 @@ namespace ArduinoJson
{ {
return p.print(c.asDouble, DIGITS); return p.print(c.asDouble, DIGITS);
} }
static JsonValue nullInstance;
}; };
} }
} }

View File

@ -18,10 +18,10 @@ namespace JsonGeneratorTests
public: public:
/* TEST_METHOD(Empty) TEST_METHOD(Empty)
{ {
mustNotContain("key"); mustNotContain("key");
}*/ }
TEST_METHOD(OneString) TEST_METHOD(OneString)
{ {
@ -35,8 +35,14 @@ namespace JsonGeneratorTests
void mustContain(const char* key, const char* expected) void mustContain(const char* key, const char* expected)
{ {
auto actual = (const char*) object[key]; const char* actual = object[key];
Assert::AreEqual(expected, actual); Assert::AreEqual(expected, actual);
} }
void mustNotContain(const char* key)
{
const char* actual = object[key];
Assert::IsNull(actual);
}
}; };
} }