mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-07-16 12:02:14 +02:00
Refactored to use StringBuilder
This commit is contained in:
@ -1,8 +1,13 @@
|
|||||||
#include "JsonBuffer.h"
|
|
||||||
#include "JsonObject.h"
|
#include "JsonObject.h"
|
||||||
|
|
||||||
|
#include <string.h> // for strcmp
|
||||||
|
|
||||||
|
#include "JsonBuffer.h"
|
||||||
#include "JsonValue.h"
|
#include "JsonValue.h"
|
||||||
#include "JsonNode.h"
|
#include "JsonNode.h"
|
||||||
#include <string.h> // for strcmp
|
#include "StringBuilder.h"
|
||||||
|
|
||||||
|
using namespace ArduinoJson::Internals;
|
||||||
|
|
||||||
//JsonValue& JsonObject::operator[](char const* key)
|
//JsonValue& JsonObject::operator[](char const* key)
|
||||||
//{
|
//{
|
||||||
@ -89,7 +94,13 @@ JsonNode* JsonObject::getOrCreateNodeAt(char const* key)
|
|||||||
return newValueNode;
|
return newValueNode;
|
||||||
}
|
}
|
||||||
|
|
||||||
void JsonObject::serialize(char* buffer, size_t bufferSize) const
|
void JsonObject::printTo(char* buffer, size_t bufferSize) const
|
||||||
{
|
{
|
||||||
strcpy_s(buffer, bufferSize, "{}");
|
StringBuilder sb(buffer, bufferSize);
|
||||||
|
printTo(sb);
|
||||||
|
}
|
||||||
|
|
||||||
|
void JsonObject::printTo(Print& p) const
|
||||||
|
{
|
||||||
|
p.print("{}");
|
||||||
}
|
}
|
@ -1,5 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include "Printable.h"
|
||||||
|
|
||||||
class JsonValue;
|
class JsonValue;
|
||||||
struct JsonNode;
|
struct JsonNode;
|
||||||
|
|
||||||
@ -24,7 +26,8 @@ public:
|
|||||||
|
|
||||||
bool operator==(const JsonObject& other) const;
|
bool operator==(const JsonObject& other) const;
|
||||||
|
|
||||||
void serialize(char* buffer, size_t bufferSize) const;
|
void printTo(char* buffer, size_t bufferSize) const;
|
||||||
|
void printTo(Print& print) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
JsonNode* _node;
|
JsonNode* _node;
|
||||||
|
40
srcs/Print.cpp
Normal file
40
srcs/Print.cpp
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
/*
|
||||||
|
* Arduino JSON library
|
||||||
|
* Benoit Blanchon 2014 - MIT License
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef ARDUINO
|
||||||
|
|
||||||
|
#include "Print.h"
|
||||||
|
#include <cstdio>
|
||||||
|
|
||||||
|
size_t Print::print(const char s[])
|
||||||
|
{
|
||||||
|
size_t n = 0;
|
||||||
|
while (*s)
|
||||||
|
{
|
||||||
|
n += write(*s++);
|
||||||
|
}
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t Print::print(double value, int digits)
|
||||||
|
{
|
||||||
|
char tmp[32];
|
||||||
|
sprintf(tmp, "%.*lg", digits+1, value);
|
||||||
|
return print(tmp);
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t Print::print(long value)
|
||||||
|
{
|
||||||
|
char tmp[32];
|
||||||
|
sprintf(tmp, "%ld", value);
|
||||||
|
return print(tmp);
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t Print::println()
|
||||||
|
{
|
||||||
|
return write('\r') + write('\n');
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
30
srcs/Print.h
Normal file
30
srcs/Print.h
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
/*
|
||||||
|
* Arduino JSON library
|
||||||
|
* Benoit Blanchon 2014 - MIT License
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#ifndef ARDUINO
|
||||||
|
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
// This class reproduces Arduino's Print
|
||||||
|
class Print
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
|
||||||
|
virtual size_t write(uint8_t) = 0;
|
||||||
|
|
||||||
|
size_t print(const char[]);
|
||||||
|
size_t print(double, int = 2);
|
||||||
|
size_t print(long);
|
||||||
|
size_t println();
|
||||||
|
};
|
||||||
|
|
||||||
|
#else
|
||||||
|
|
||||||
|
#include <Print.h>
|
||||||
|
|
||||||
|
#endif
|
17
srcs/StringBuilder.cpp
Normal file
17
srcs/StringBuilder.cpp
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
/*
|
||||||
|
* Arduino JSON library
|
||||||
|
* Benoit Blanchon 2014 - MIT License
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "StringBuilder.h"
|
||||||
|
|
||||||
|
using namespace ArduinoJson::Internals;
|
||||||
|
|
||||||
|
size_t StringBuilder::write(uint8_t c)
|
||||||
|
{
|
||||||
|
if (length >= capacity) return 0;
|
||||||
|
|
||||||
|
buffer[length++] = c;
|
||||||
|
buffer[length] = 0;
|
||||||
|
return 1;
|
||||||
|
}
|
31
srcs/StringBuilder.h
Normal file
31
srcs/StringBuilder.h
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
/*
|
||||||
|
* Arduino JSON library
|
||||||
|
* Benoit Blanchon 2014 - MIT License
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "Print.h"
|
||||||
|
|
||||||
|
namespace ArduinoJson
|
||||||
|
{
|
||||||
|
namespace Internals
|
||||||
|
{
|
||||||
|
class StringBuilder : public Print
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
StringBuilder(char* buf, int size)
|
||||||
|
: buffer(buf), capacity(size - 1), length(0)
|
||||||
|
{
|
||||||
|
buffer[0] = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual size_t write(uint8_t c);
|
||||||
|
|
||||||
|
private:
|
||||||
|
char* buffer;
|
||||||
|
int capacity;
|
||||||
|
int length;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
@ -45,6 +45,7 @@
|
|||||||
<WarningLevel>Level3</WarningLevel>
|
<WarningLevel>Level3</WarningLevel>
|
||||||
<Optimization>Disabled</Optimization>
|
<Optimization>Disabled</Optimization>
|
||||||
<SDLCheck>true</SDLCheck>
|
<SDLCheck>true</SDLCheck>
|
||||||
|
<PreprocessorDefinitions>_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
<Link>
|
<Link>
|
||||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||||
@ -57,6 +58,7 @@
|
|||||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||||
<SDLCheck>true</SDLCheck>
|
<SDLCheck>true</SDLCheck>
|
||||||
|
<PreprocessorDefinitions>_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
<Link>
|
<Link>
|
||||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||||
@ -69,12 +71,16 @@
|
|||||||
<ClInclude Include="JsonNode.h" />
|
<ClInclude Include="JsonNode.h" />
|
||||||
<ClInclude Include="JsonObject.h" />
|
<ClInclude Include="JsonObject.h" />
|
||||||
<ClInclude Include="JsonValue.h" />
|
<ClInclude Include="JsonValue.h" />
|
||||||
|
<ClInclude Include="Print.h" />
|
||||||
<ClInclude Include="StaticJsonBuffer.h" />
|
<ClInclude Include="StaticJsonBuffer.h" />
|
||||||
|
<ClInclude Include="StringBuilder.h" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClCompile Include="JsonBuffer.cpp" />
|
<ClCompile Include="JsonBuffer.cpp" />
|
||||||
<ClCompile Include="JsonObject.cpp" />
|
<ClCompile Include="JsonObject.cpp" />
|
||||||
<ClCompile Include="JsonValue.cpp" />
|
<ClCompile Include="JsonValue.cpp" />
|
||||||
|
<ClCompile Include="Print.cpp" />
|
||||||
|
<ClCompile Include="StringBuilder.cpp" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||||
<ImportGroup Label="ExtensionTargets">
|
<ImportGroup Label="ExtensionTargets">
|
||||||
|
@ -30,6 +30,12 @@
|
|||||||
<ClInclude Include="JsonNode.h">
|
<ClInclude Include="JsonNode.h">
|
||||||
<Filter>Header Files</Filter>
|
<Filter>Header Files</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
<ClInclude Include="StringBuilder.h">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
<ClInclude Include="Print.h">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClCompile Include="JsonObject.cpp">
|
<ClCompile Include="JsonObject.cpp">
|
||||||
@ -41,5 +47,11 @@
|
|||||||
<ClCompile Include="JsonValue.cpp">
|
<ClCompile Include="JsonValue.cpp">
|
||||||
<Filter>Source Files</Filter>
|
<Filter>Source Files</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="StringBuilder.cpp">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include="Print.cpp">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
@ -13,7 +13,7 @@ protected:
|
|||||||
void jsonMustBe(const char* expected)
|
void jsonMustBe(const char* expected)
|
||||||
{
|
{
|
||||||
char actual[256];
|
char actual[256];
|
||||||
object.serialize(actual, sizeof(actual));
|
object.printTo(actual, sizeof(actual));
|
||||||
|
|
||||||
EXPECT_STREQ(expected, actual);
|
EXPECT_STREQ(expected, actual);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user