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::Internals;
JsonValue JsonObjectBase::nullValue;
size_t JsonObjectBase::printTo(Print& p) const
{
size_t n = 0;
@ -51,10 +55,19 @@ JsonValue& JsonObjectBase::operator[](char const* key)
p++;
}
if (count >= capacity)
return JsonValue::null();
JsonValue* value;
if (count < capacity)
{
count++;
p->key = key;
return p->value;
value = &p->value;
}
else
{
value = &nullValue;
}
value->reset();
return *value;
}

View File

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

View File

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

View File

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

View File

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