forked from bblanchon/ArduinoJson
Extracted class JsonContainer
This commit is contained in:
@ -41,7 +41,7 @@ struct JsonNode
|
|||||||
{
|
{
|
||||||
JsonNode* child;
|
JsonNode* child;
|
||||||
JsonBuffer* buffer;
|
JsonBuffer* buffer;
|
||||||
} asObject;
|
} asContainer;
|
||||||
|
|
||||||
struct
|
struct
|
||||||
{
|
{
|
||||||
|
@ -41,7 +41,7 @@ size_t JsonNodeSerializer::serializeObject(const JsonNode* node)
|
|||||||
|
|
||||||
n += _sink.write('{');
|
n += _sink.write('{');
|
||||||
|
|
||||||
JsonNode* firstChild = node->content.asObject.child;
|
JsonNode* firstChild = node->content.asContainer.child;
|
||||||
|
|
||||||
for (JsonNode* child = firstChild; child; child = child->next)
|
for (JsonNode* child = firstChild; child; child = child->next)
|
||||||
{
|
{
|
||||||
|
@ -11,7 +11,7 @@ JsonObject JsonBuffer::createObject()
|
|||||||
JsonNode* node = createNode(JSON_OBJECT);
|
JsonNode* node = createNode(JSON_OBJECT);
|
||||||
|
|
||||||
if (node)
|
if (node)
|
||||||
node->content.asObject.buffer = this;
|
node->content.asContainer.buffer = this;
|
||||||
|
|
||||||
return JsonObject(node);
|
return JsonObject(node);
|
||||||
}
|
}
|
||||||
|
16
srcs/JsonContainer.cpp
Normal file
16
srcs/JsonContainer.cpp
Normal 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
26
srcs/JsonContainer.h
Normal 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;
|
||||||
|
};
|
||||||
|
|
@ -13,7 +13,7 @@ using namespace ArduinoJson::Internals;
|
|||||||
|
|
||||||
size_t JsonObject::size()
|
size_t JsonObject::size()
|
||||||
{
|
{
|
||||||
JsonNode* firstChild = _node->content.asObject.child;
|
JsonNode* firstChild = _node->content.asContainer.child;
|
||||||
|
|
||||||
int size = 0;
|
int size = 0;
|
||||||
|
|
||||||
@ -33,7 +33,7 @@ JsonValue JsonObject::operator[](char const* key)
|
|||||||
|
|
||||||
void JsonObject::remove(char const* key)
|
void JsonObject::remove(char const* key)
|
||||||
{
|
{
|
||||||
JsonNode* firstChild = _node->content.asObject.child;
|
JsonNode* firstChild = _node->content.asContainer.child;
|
||||||
JsonNode* lastChild = 0;
|
JsonNode* lastChild = 0;
|
||||||
|
|
||||||
for (JsonNode* child = firstChild; child; child = child->next)
|
for (JsonNode* child = firstChild; child; child = child->next)
|
||||||
@ -45,7 +45,7 @@ void JsonObject::remove(char const* key)
|
|||||||
if (lastChild)
|
if (lastChild)
|
||||||
lastChild->next = child->next;
|
lastChild->next = child->next;
|
||||||
else
|
else
|
||||||
_node->content.asObject.child = child->next;
|
_node->content.asContainer.child = child->next;
|
||||||
}
|
}
|
||||||
lastChild = child;
|
lastChild = child;
|
||||||
}
|
}
|
||||||
@ -60,7 +60,7 @@ JsonNode* JsonObject::getOrCreateNodeAt(char const* key)
|
|||||||
{
|
{
|
||||||
if (!_node || _node->type != JSON_OBJECT) return 0;
|
if (!_node || _node->type != JSON_OBJECT) return 0;
|
||||||
|
|
||||||
JsonNode* firstChild = _node->content.asObject.child;
|
JsonNode* firstChild = _node->content.asContainer.child;
|
||||||
JsonNode* lastChild = 0;
|
JsonNode* lastChild = 0;
|
||||||
|
|
||||||
for (JsonNode* child = firstChild; child; child = child->next)
|
for (JsonNode* child = firstChild; child; child = child->next)
|
||||||
@ -73,7 +73,7 @@ JsonNode* JsonObject::getOrCreateNodeAt(char const* key)
|
|||||||
lastChild = child;
|
lastChild = child;
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonBuffer* buffer = _node->content.asObject.buffer;
|
JsonBuffer* buffer = _node->content.asContainer.buffer;
|
||||||
|
|
||||||
JsonNode* newValueNode = buffer->createNode(JSON_UNDEFINED);
|
JsonNode* newValueNode = buffer->createNode(JSON_UNDEFINED);
|
||||||
if (!newValueNode) return 0;
|
if (!newValueNode) return 0;
|
||||||
@ -87,19 +87,7 @@ JsonNode* JsonObject::getOrCreateNodeAt(char const* key)
|
|||||||
if (lastChild)
|
if (lastChild)
|
||||||
lastChild->next = newKeyNode;
|
lastChild->next = newKeyNode;
|
||||||
else
|
else
|
||||||
_node->content.asObject.child = newKeyNode;
|
_node->content.asContainer.child = newKeyNode;
|
||||||
|
|
||||||
return newValueNode;
|
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);
|
|
||||||
}
|
|
@ -1,22 +1,21 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "Arduino/Printable.h"
|
#include "JsonContainer.h"
|
||||||
|
|
||||||
class JsonValue;
|
class JsonValue;
|
||||||
struct JsonNode;
|
struct JsonNode;
|
||||||
|
|
||||||
class JsonObject : public Printable
|
class JsonObject : public JsonContainer
|
||||||
{
|
{
|
||||||
friend JsonValue;
|
friend JsonValue;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
JsonObject()
|
JsonObject()
|
||||||
: _node(0)
|
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonObject(JsonNode* node)
|
explicit JsonObject(JsonNode* node)
|
||||||
: _node(node)
|
: JsonContainer(node)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -27,11 +26,6 @@ public:
|
|||||||
|
|
||||||
bool operator==(const JsonObject& other) const;
|
bool operator==(const JsonObject& other) const;
|
||||||
|
|
||||||
size_t printTo(char* buffer, size_t bufferSize) const;
|
|
||||||
virtual size_t printTo(Print& print) const;
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
JsonNode* _node;
|
|
||||||
|
|
||||||
JsonNode* getOrCreateNodeAt(char const* key);
|
JsonNode* getOrCreateNodeAt(char const* key);
|
||||||
};
|
};
|
@ -78,6 +78,7 @@
|
|||||||
<ClInclude Include="JsonBuffer.h" />
|
<ClInclude Include="JsonBuffer.h" />
|
||||||
<ClInclude Include="Internals\JsonNode.h" />
|
<ClInclude Include="Internals\JsonNode.h" />
|
||||||
<ClInclude Include="Internals\JsonNodeSerializer.h" />
|
<ClInclude Include="Internals\JsonNodeSerializer.h" />
|
||||||
|
<ClInclude Include="JsonContainer.h" />
|
||||||
<ClInclude Include="JsonObject.h" />
|
<ClInclude Include="JsonObject.h" />
|
||||||
<ClInclude Include="JsonValue.h" />
|
<ClInclude Include="JsonValue.h" />
|
||||||
<ClInclude Include="Arduino\Print.h" />
|
<ClInclude Include="Arduino\Print.h" />
|
||||||
@ -89,6 +90,7 @@
|
|||||||
<ClCompile Include="Internals\EscapedString.cpp" />
|
<ClCompile Include="Internals\EscapedString.cpp" />
|
||||||
<ClCompile Include="JsonBuffer.cpp" />
|
<ClCompile Include="JsonBuffer.cpp" />
|
||||||
<ClCompile Include="Internals\JsonNodeSerializer.cpp" />
|
<ClCompile Include="Internals\JsonNodeSerializer.cpp" />
|
||||||
|
<ClCompile Include="JsonContainer.cpp" />
|
||||||
<ClCompile Include="JsonObject.cpp" />
|
<ClCompile Include="JsonObject.cpp" />
|
||||||
<ClCompile Include="JsonValue.cpp" />
|
<ClCompile Include="JsonValue.cpp" />
|
||||||
<ClCompile Include="Arduino\Print.cpp" />
|
<ClCompile Include="Arduino\Print.cpp" />
|
||||||
|
@ -45,6 +45,9 @@
|
|||||||
<ClInclude Include="Arduino\Printable.h">
|
<ClInclude Include="Arduino\Printable.h">
|
||||||
<Filter>Header Files</Filter>
|
<Filter>Header Files</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
<ClInclude Include="JsonContainer.h">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClCompile Include="JsonObject.cpp">
|
<ClCompile Include="JsonObject.cpp">
|
||||||
@ -68,5 +71,8 @@
|
|||||||
<ClCompile Include="Arduino\Print.cpp">
|
<ClCompile Include="Arduino\Print.cpp">
|
||||||
<Filter>Source Files</Filter>
|
<Filter>Source Files</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="JsonContainer.cpp">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
Reference in New Issue
Block a user