forked from bblanchon/ArduinoJson
Added interface Printable.
This commit is contained in:
@ -37,7 +37,7 @@ private:
|
|||||||
JsonValue items[N];
|
JsonValue items[N];
|
||||||
int itemCount;
|
int itemCount;
|
||||||
|
|
||||||
virtual size_t writeTo(Print& p)
|
virtual size_t writeTo(Print& p) const
|
||||||
{
|
{
|
||||||
size_t n = 0;
|
size_t n = 0;
|
||||||
|
|
||||||
|
@ -94,6 +94,7 @@
|
|||||||
<ClInclude Include="JsonObjectBase.h" />
|
<ClInclude Include="JsonObjectBase.h" />
|
||||||
<ClInclude Include="Print.h" />
|
<ClInclude Include="Print.h" />
|
||||||
<ClInclude Include="JsonValue.h" />
|
<ClInclude Include="JsonValue.h" />
|
||||||
|
<ClInclude Include="Printable.h" />
|
||||||
<ClInclude Include="StringBuilder.h" />
|
<ClInclude Include="StringBuilder.h" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||||
|
@ -50,5 +50,8 @@
|
|||||||
<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>
|
||||||
</Project>
|
</Project>
|
@ -45,7 +45,7 @@ private:
|
|||||||
KeyValuePair items[N];
|
KeyValuePair items[N];
|
||||||
int itemCount;
|
int itemCount;
|
||||||
|
|
||||||
virtual size_t writeTo(Print& p)
|
virtual size_t writeTo(Print& p) const
|
||||||
{
|
{
|
||||||
size_t n = 0;
|
size_t n = 0;
|
||||||
|
|
||||||
|
@ -7,8 +7,9 @@
|
|||||||
|
|
||||||
#include "JsonValue.h"
|
#include "JsonValue.h"
|
||||||
#include "Print.h"
|
#include "Print.h"
|
||||||
|
#include "Printable.h"
|
||||||
|
|
||||||
class JsonObjectBase
|
class JsonObjectBase : public Printable
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
@ -18,6 +19,6 @@ public:
|
|||||||
return writeTo(sb);
|
return writeTo(sb);
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual size_t writeTo(Print& p) = 0;
|
virtual size_t writeTo(Print& p) const = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -8,12 +8,12 @@
|
|||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
|
||||||
size_t JsonValue::writeBooleanTo(Print& p)
|
size_t JsonValue::writeBooleanTo(Print& p) const
|
||||||
{
|
{
|
||||||
return p.write(content.boolean ? "true" : "false");
|
return p.write(content.boolean ? "true" : "false");
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t JsonValue::writeNumberTo(Print& p)
|
size_t JsonValue::writeNumberTo(Print& p) const
|
||||||
{
|
{
|
||||||
char tmp[16];
|
char tmp[16];
|
||||||
|
|
||||||
@ -22,7 +22,7 @@ size_t JsonValue::writeNumberTo(Print& p)
|
|||||||
return p.write(tmp);
|
return p.write(tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t JsonValue::writeObjectTo(Print& p)
|
size_t JsonValue::writeObjectTo(Print& p) const
|
||||||
{
|
{
|
||||||
if (content.object)
|
if (content.object)
|
||||||
return ((JsonObjectBase*)content.object)->writeTo(p);
|
return ((JsonObjectBase*)content.object)->writeTo(p);
|
||||||
@ -30,7 +30,7 @@ size_t JsonValue::writeObjectTo(Print& p)
|
|||||||
return p.write("null");
|
return p.write("null");
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t JsonValue::writeStringTo(Print& p)
|
size_t JsonValue::writeStringTo(Print& p) const
|
||||||
{
|
{
|
||||||
auto s = content.string;
|
auto s = content.string;
|
||||||
|
|
||||||
|
@ -5,11 +5,10 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include "Printable.h"
|
||||||
#include "StringBuilder.h"
|
#include "StringBuilder.h"
|
||||||
|
|
||||||
class JsonObjectBase;
|
class JsonValue : public Printable
|
||||||
|
|
||||||
class JsonValue
|
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
@ -35,13 +34,13 @@ public:
|
|||||||
content.boolean = value;
|
content.boolean = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonValue(JsonObjectBase& value)
|
JsonValue(Printable& value)
|
||||||
: implementation(&JsonValue::writeObjectTo)
|
: implementation(&JsonValue::writeObjectTo)
|
||||||
{
|
{
|
||||||
content.object = &value;
|
content.object = &value;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t writeTo(Print& p)
|
virtual size_t writeTo(Print& p) const
|
||||||
{
|
{
|
||||||
// handmade polymorphism
|
// handmade polymorphism
|
||||||
return (this->*implementation)(p);
|
return (this->*implementation)(p);
|
||||||
@ -53,16 +52,16 @@ private:
|
|||||||
{
|
{
|
||||||
bool boolean;
|
bool boolean;
|
||||||
double number;
|
double number;
|
||||||
JsonObjectBase* object;
|
Printable* object;
|
||||||
const char* string;
|
const char* string;
|
||||||
};
|
};
|
||||||
|
|
||||||
Content content;
|
Content content;
|
||||||
|
|
||||||
size_t(JsonValue::*implementation)(Print& p);
|
size_t(JsonValue::*implementation)(Print& p)const;
|
||||||
|
|
||||||
size_t writeBooleanTo(Print& p);
|
size_t writeBooleanTo(Print& p) const;
|
||||||
size_t writeNumberTo(Print& p);
|
size_t writeNumberTo(Print& p) const;
|
||||||
size_t writeObjectTo(Print& p);
|
size_t writeObjectTo(Print& p) const;
|
||||||
size_t writeStringTo(Print& p);
|
size_t writeStringTo(Print& p) const;
|
||||||
};
|
};
|
16
JsonGeneratorTests/Printable.h
Normal file
16
JsonGeneratorTests/Printable.h
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
/*
|
||||||
|
* Arduino JSON library
|
||||||
|
* Benoit Blanchon 2014 - MIT License
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
class Print;
|
||||||
|
|
||||||
|
class Printable
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
|
||||||
|
virtual size_t writeTo(Print& p) const = 0;
|
||||||
|
};
|
||||||
|
|
Reference in New Issue
Block a user