Extracted class JsonContainer

This commit is contained in:
Benoit Blanchon
2014-10-01 16:13:36 +02:00
parent d66a7adc22
commit b0e43f7538
9 changed files with 63 additions and 31 deletions

View File

@ -41,7 +41,7 @@ struct JsonNode
{
JsonNode* child;
JsonBuffer* buffer;
} asObject;
} asContainer;
struct
{

View File

@ -41,7 +41,7 @@ size_t JsonNodeSerializer::serializeObject(const JsonNode* node)
n += _sink.write('{');
JsonNode* firstChild = node->content.asObject.child;
JsonNode* firstChild = node->content.asContainer.child;
for (JsonNode* child = firstChild; child; child = child->next)
{

View File

@ -11,7 +11,7 @@ JsonObject JsonBuffer::createObject()
JsonNode* node = createNode(JSON_OBJECT);
if (node)
node->content.asObject.buffer = this;
node->content.asContainer.buffer = this;
return JsonObject(node);
}

16
srcs/JsonContainer.cpp Normal file
View File

@ -0,0 +1,16 @@
#include "JsonContainer.h"
#include "Internals/JsonNodeSerializer.h"
#include "Internals/StringBuilder.h"
size_t JsonContainer::printTo(char* buffer, size_t bufferSize) const
{
ArduinoJson::Internals::StringBuilder sb(buffer, bufferSize);
return printTo(sb);
}
size_t JsonContainer::printTo(Print& p) const
{
JsonNodeSerializer serializer(p);
return serializer.serialize(_node);
}

26
srcs/JsonContainer.h Normal file
View File

@ -0,0 +1,26 @@
#pragma once
#include "Arduino\Printable.h"
struct JsonNode;
class JsonContainer : public Printable
{
public:
JsonContainer()
: _node(0)
{
}
JsonContainer(JsonNode* node)
: _node(node)
{
}
size_t printTo(char* buffer, size_t bufferSize) const;
virtual size_t printTo(Print& print) const;
protected:
JsonNode* _node;
};

View File

@ -13,7 +13,7 @@ using namespace ArduinoJson::Internals;
size_t JsonObject::size()
{
JsonNode* firstChild = _node->content.asObject.child;
JsonNode* firstChild = _node->content.asContainer.child;
int size = 0;
@ -33,7 +33,7 @@ JsonValue JsonObject::operator[](char const* key)
void JsonObject::remove(char const* key)
{
JsonNode* firstChild = _node->content.asObject.child;
JsonNode* firstChild = _node->content.asContainer.child;
JsonNode* lastChild = 0;
for (JsonNode* child = firstChild; child; child = child->next)
@ -45,7 +45,7 @@ void JsonObject::remove(char const* key)
if (lastChild)
lastChild->next = child->next;
else
_node->content.asObject.child = child->next;
_node->content.asContainer.child = child->next;
}
lastChild = child;
}
@ -60,7 +60,7 @@ JsonNode* JsonObject::getOrCreateNodeAt(char const* key)
{
if (!_node || _node->type != JSON_OBJECT) return 0;
JsonNode* firstChild = _node->content.asObject.child;
JsonNode* firstChild = _node->content.asContainer.child;
JsonNode* lastChild = 0;
for (JsonNode* child = firstChild; child; child = child->next)
@ -73,7 +73,7 @@ JsonNode* JsonObject::getOrCreateNodeAt(char const* key)
lastChild = child;
}
JsonBuffer* buffer = _node->content.asObject.buffer;
JsonBuffer* buffer = _node->content.asContainer.buffer;
JsonNode* newValueNode = buffer->createNode(JSON_UNDEFINED);
if (!newValueNode) return 0;
@ -87,19 +87,7 @@ JsonNode* JsonObject::getOrCreateNodeAt(char const* key)
if (lastChild)
lastChild->next = newKeyNode;
else
_node->content.asObject.child = newKeyNode;
_node->content.asContainer.child = newKeyNode;
return newValueNode;
}
size_t JsonObject::printTo(char* buffer, size_t bufferSize) const
{
StringBuilder sb(buffer, bufferSize);
return printTo(sb);
}
size_t JsonObject::printTo(Print& p) const
{
JsonNodeSerializer serializer(p);
return serializer.serialize(_node);
}

View File

@ -1,22 +1,21 @@
#pragma once
#include "Arduino/Printable.h"
#include "JsonContainer.h"
class JsonValue;
struct JsonNode;
class JsonObject : public Printable
class JsonObject : public JsonContainer
{
friend JsonValue;
public:
JsonObject()
: _node(0)
{
}
JsonObject(JsonNode* node)
: _node(node)
explicit JsonObject(JsonNode* node)
: JsonContainer(node)
{
}
@ -27,11 +26,6 @@ public:
bool operator==(const JsonObject& other) const;
size_t printTo(char* buffer, size_t bufferSize) const;
virtual size_t printTo(Print& print) const;
private:
JsonNode* _node;
JsonNode* getOrCreateNodeAt(char const* key);
};

View File

@ -78,6 +78,7 @@
<ClInclude Include="JsonBuffer.h" />
<ClInclude Include="Internals\JsonNode.h" />
<ClInclude Include="Internals\JsonNodeSerializer.h" />
<ClInclude Include="JsonContainer.h" />
<ClInclude Include="JsonObject.h" />
<ClInclude Include="JsonValue.h" />
<ClInclude Include="Arduino\Print.h" />
@ -89,6 +90,7 @@
<ClCompile Include="Internals\EscapedString.cpp" />
<ClCompile Include="JsonBuffer.cpp" />
<ClCompile Include="Internals\JsonNodeSerializer.cpp" />
<ClCompile Include="JsonContainer.cpp" />
<ClCompile Include="JsonObject.cpp" />
<ClCompile Include="JsonValue.cpp" />
<ClCompile Include="Arduino\Print.cpp" />

View File

@ -45,6 +45,9 @@
<ClInclude Include="Arduino\Printable.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="JsonContainer.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="JsonObject.cpp">
@ -68,5 +71,8 @@
<ClCompile Include="Arduino\Print.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="JsonContainer.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
</Project>