forked from bblanchon/ArduinoJson
Implement Printable
This commit is contained in:
@ -9,33 +9,6 @@
|
|||||||
|
|
||||||
using namespace ArduinoJson::Internals;
|
using namespace ArduinoJson::Internals;
|
||||||
|
|
||||||
//JsonValue& JsonObject::operator[](char const* key)
|
|
||||||
//{
|
|
||||||
// addNodeAt(key, innerObject._node);
|
|
||||||
// return innerObject;
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//void JsonObject::addNodeAt(const char* key, JsonNode& node)
|
|
||||||
//{
|
|
||||||
// JsonNode& keyNode = _buffer.createNode();
|
|
||||||
// keyNode.becomeKey(key, node);
|
|
||||||
// appendChild(keyNode);
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//void JsonObject::appendChild(JsonNode& newChild)
|
|
||||||
//{
|
|
||||||
// JsonNode* lastChild = _node.asObjectNode.child;
|
|
||||||
// while (lastChild->next)
|
|
||||||
// {
|
|
||||||
// lastChild = lastChild->next;
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// if (lastChild)
|
|
||||||
// lastChild->next = &newChild;
|
|
||||||
// else
|
|
||||||
// _node.asObjectNode.child = &newChild;
|
|
||||||
//}
|
|
||||||
|
|
||||||
size_t JsonObject::size()
|
size_t JsonObject::size()
|
||||||
{
|
{
|
||||||
JsonNode* firstChild = _node->content.asObject.child;
|
JsonNode* firstChild = _node->content.asObject.child;
|
||||||
@ -94,13 +67,13 @@ JsonNode* JsonObject::getOrCreateNodeAt(char const* key)
|
|||||||
return newValueNode;
|
return newValueNode;
|
||||||
}
|
}
|
||||||
|
|
||||||
void JsonObject::printTo(char* buffer, size_t bufferSize) const
|
size_t JsonObject::printTo(char* buffer, size_t bufferSize) const
|
||||||
{
|
{
|
||||||
StringBuilder sb(buffer, bufferSize);
|
StringBuilder sb(buffer, bufferSize);
|
||||||
printTo(sb);
|
return printTo(sb);
|
||||||
}
|
}
|
||||||
|
|
||||||
void JsonObject::printTo(Print& p) const
|
size_t JsonObject::printTo(Print& p) const
|
||||||
{
|
{
|
||||||
p.print("{}");
|
return p.print("{}");
|
||||||
}
|
}
|
@ -5,7 +5,7 @@
|
|||||||
class JsonValue;
|
class JsonValue;
|
||||||
struct JsonNode;
|
struct JsonNode;
|
||||||
|
|
||||||
class JsonObject
|
class JsonObject : public Printable
|
||||||
{
|
{
|
||||||
friend JsonValue;
|
friend JsonValue;
|
||||||
|
|
||||||
@ -26,8 +26,8 @@ public:
|
|||||||
|
|
||||||
bool operator==(const JsonObject& other) const;
|
bool operator==(const JsonObject& other) const;
|
||||||
|
|
||||||
void printTo(char* buffer, size_t bufferSize) const;
|
size_t printTo(char* buffer, size_t bufferSize) const;
|
||||||
void printTo(Print& print) const;
|
virtual size_t printTo(Print& print) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
JsonNode* _node;
|
JsonNode* _node;
|
||||||
|
24
srcs/Printable.h
Normal file
24
srcs/Printable.h
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
/*
|
||||||
|
* Arduino JSON library
|
||||||
|
* Benoit Blanchon 2014 - MIT License
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#ifndef ARDUINO
|
||||||
|
|
||||||
|
class Print;
|
||||||
|
|
||||||
|
class Printable
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
|
||||||
|
virtual size_t printTo(Print& p) const = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
#else
|
||||||
|
|
||||||
|
#include <Printable.h>
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
@ -72,6 +72,7 @@
|
|||||||
<ClInclude Include="JsonObject.h" />
|
<ClInclude Include="JsonObject.h" />
|
||||||
<ClInclude Include="JsonValue.h" />
|
<ClInclude Include="JsonValue.h" />
|
||||||
<ClInclude Include="Print.h" />
|
<ClInclude Include="Print.h" />
|
||||||
|
<ClInclude Include="Printable.h" />
|
||||||
<ClInclude Include="StaticJsonBuffer.h" />
|
<ClInclude Include="StaticJsonBuffer.h" />
|
||||||
<ClInclude Include="StringBuilder.h" />
|
<ClInclude Include="StringBuilder.h" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
@ -36,6 +36,9 @@
|
|||||||
<ClInclude Include="Print.h">
|
<ClInclude Include="Print.h">
|
||||||
<Filter>Header Files</Filter>
|
<Filter>Header Files</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
<ClInclude Include="Printable.h">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClCompile Include="JsonObject.cpp">
|
<ClCompile Include="JsonObject.cpp">
|
||||||
|
@ -13,9 +13,10 @@ protected:
|
|||||||
void jsonMustBe(const char* expected)
|
void jsonMustBe(const char* expected)
|
||||||
{
|
{
|
||||||
char actual[256];
|
char actual[256];
|
||||||
object.printTo(actual, sizeof(actual));
|
int result = object.printTo(actual, sizeof(actual));
|
||||||
|
|
||||||
EXPECT_STREQ(expected, actual);
|
EXPECT_STREQ(expected, actual);
|
||||||
|
EXPECT_EQ(strlen(expected), result);
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonObject object;
|
JsonObject object;
|
||||||
|
Reference in New Issue
Block a user