mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-06-29 03:11:00 +02:00
Compare commits
88 Commits
Author | SHA1 | Date | |
---|---|---|---|
58c051f564 | |||
763aa7fe37 | |||
cd88fb0882 | |||
ec843659d8 | |||
2997a405a0 | |||
57f28c2017 | |||
b3b70b78cf | |||
48018bd6e6 | |||
61952a9bcd | |||
602cc104f9 | |||
d71a39211d | |||
f77a8b02e3 | |||
aa2cd0db00 | |||
f127ef6019 | |||
3ae7327687 | |||
23e61cc0f7 | |||
b5002265cf | |||
e48ea94789 | |||
6539c6982c | |||
d877d77b63 | |||
1df6cde026 | |||
fafae8181b | |||
151fc52c1c | |||
ea79340dc7 | |||
9e88514700 | |||
752378a8cb | |||
8465cc0c83 | |||
2ddf8f1619 | |||
f7aa0f89e3 | |||
3d322fdb28 | |||
981adf1989 | |||
dbc3bee3a0 | |||
3f2b7b706a | |||
c243417585 | |||
514a6c0879 | |||
76f9ecce75 | |||
410ca55e88 | |||
66c05041e8 | |||
aafabd8e8d | |||
eb1a774778 | |||
75c89e7b35 | |||
e31a2136fc | |||
380722402f | |||
030c8542e7 | |||
1f25d4434e | |||
f29904e217 | |||
7246db7691 | |||
4bdbc6c1fc | |||
8e6fdb20eb | |||
016d0d699e | |||
6771603a05 | |||
d067cf0e84 | |||
13593d73a3 | |||
bc86ae800a | |||
df52dceaa1 | |||
d460b59b50 | |||
8e5ea91f8d | |||
4a8b7d0cb4 | |||
96c9b5deee | |||
8e81b9bb26 | |||
817cc09975 | |||
1bc45f1fd7 | |||
d2fe9ddf49 | |||
5cc06180e6 | |||
65e8b6d405 | |||
09294cb5e6 | |||
158f4600fb | |||
0d28612507 | |||
7c99d4d63d | |||
1a01800782 | |||
6384bc414a | |||
c10bcee324 | |||
028ff6676e | |||
23b5237f74 | |||
88510705be | |||
15d3068d78 | |||
ae6beb9340 | |||
c1f4128ccd | |||
5fb6edfc91 | |||
2771b830b7 | |||
84aa627038 | |||
4528b8fc95 | |||
60c6f2db47 | |||
13c386c7a3 | |||
7877ee1b4c | |||
2c29327ebd | |||
85ffb83aa6 | |||
1ce6661fa6 |
13
BuildArduinoPackage.sh
Normal file
13
BuildArduinoPackage.sh
Normal file
@ -0,0 +1,13 @@
|
||||
#!/bin/bash
|
||||
|
||||
ZIP="C:\Program Files\7-Zip\7z.exe"
|
||||
|
||||
TAG=$(git describe)
|
||||
OUTPUT="ArduinoJson-$TAG.zip"
|
||||
|
||||
cd ..
|
||||
|
||||
INPUT=$(find ArduinoJson -regex ".*\.\(cpp\|h\|md\|txt\|ino\)$" -not -regex ".*Tests/.*")
|
||||
|
||||
rm -f $OUTPUT
|
||||
"$ZIP" a $OUTPUT $INPUT
|
38
CHANGELOG.md
38
CHANGELOG.md
@ -1,6 +1,44 @@
|
||||
Arduino JSON: change log
|
||||
========================
|
||||
|
||||
v3.3
|
||||
----
|
||||
|
||||
* Added indented output for the JSON generator, see example bellow.
|
||||
* Added `IndentedPrint`, a decorator for `Print` to allow indented output
|
||||
|
||||
Example:
|
||||
|
||||
JsonOject<2> json;
|
||||
json["key"] = "value";
|
||||
json.prettyPrintTo(Serial);
|
||||
|
||||
v3.2
|
||||
----
|
||||
|
||||
* Fixed a bug when adding nested object in `JsonArray` (bug introduced in v3.1).
|
||||
|
||||
v3.1
|
||||
----
|
||||
|
||||
* Calling `Generator::JsonObject::add()` twice with the same `key` now replaces the `value`
|
||||
* Added `Generator::JsonObject::operator[]`, see bellow the new API
|
||||
* Added `Generator::JsonObject::remove()`
|
||||
|
||||
Old generator API:
|
||||
|
||||
JsonObject<3> root;
|
||||
root.add("sensor", "gps");
|
||||
root.add("time", 1351824120);
|
||||
root.add("data", array);
|
||||
|
||||
New generator API:
|
||||
|
||||
JsonObject<3> root;
|
||||
root["sensor"] = "gps";
|
||||
root["time"] = 1351824120;
|
||||
root["data"] = array;
|
||||
|
||||
v3.0
|
||||
----
|
||||
|
||||
|
@ -6,7 +6,10 @@
|
||||
// This file is here to help the Arduino IDE find the .cpp files
|
||||
|
||||
#include "JsonGenerator/EscapedString.cpp"
|
||||
#include "JsonGenerator/IndentedPrint.cpp"
|
||||
#include "JsonGenerator/JsonArrayBase.cpp"
|
||||
#include "JsonGenerator/JsonObjectBase.cpp"
|
||||
#include "JsonGenerator/JsonValue.cpp"
|
||||
#include "JsonGenerator/JsonPrettyPrint.cpp"
|
||||
#include "JsonGenerator/JsonPrintable.cpp"
|
||||
#include "JsonGenerator/StringBuilder.cpp"
|
@ -30,10 +30,8 @@ static inline size_t printCharTo(char c, Print& p)
|
||||
: p.write(c);
|
||||
}
|
||||
|
||||
size_t EscapedString::printTo(Print& p) const
|
||||
size_t EscapedString::printTo(const char* s, Print& p)
|
||||
{
|
||||
const char* s = rawString;
|
||||
|
||||
if (!s) return p.print("null");
|
||||
|
||||
size_t n = p.write('\"');
|
||||
|
@ -14,16 +14,7 @@ namespace ArduinoJson
|
||||
class EscapedString
|
||||
{
|
||||
public:
|
||||
|
||||
void set(const char* s)
|
||||
{
|
||||
rawString = s;
|
||||
}
|
||||
|
||||
size_t printTo(Print&) const;
|
||||
|
||||
private:
|
||||
const char* rawString;
|
||||
static size_t printTo(const char*, Print&);
|
||||
};
|
||||
}
|
||||
}
|
45
JsonGenerator/IndentedPrint.cpp
Normal file
45
JsonGenerator/IndentedPrint.cpp
Normal file
@ -0,0 +1,45 @@
|
||||
#include "IndentedPrint.h"
|
||||
|
||||
using namespace ArduinoJson::Generator;
|
||||
|
||||
void IndentedPrint::indent()
|
||||
{
|
||||
if (level < MAX_LEVEL)
|
||||
level++;
|
||||
}
|
||||
|
||||
void IndentedPrint::unindent()
|
||||
{
|
||||
if (level > 0)
|
||||
level--;
|
||||
}
|
||||
|
||||
void IndentedPrint::setTabSize(uint8_t n)
|
||||
{
|
||||
if (n < MAX_TAB_SIZE)
|
||||
tabSize = n;
|
||||
}
|
||||
|
||||
size_t IndentedPrint::write(uint8_t c)
|
||||
{
|
||||
size_t n = 0;
|
||||
|
||||
if (isNewLine)
|
||||
n += writeTabs();
|
||||
|
||||
n += sink.write(c);
|
||||
|
||||
isNewLine = c == '\n';
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
inline size_t IndentedPrint::writeTabs()
|
||||
{
|
||||
size_t n = 0;
|
||||
|
||||
for (int i = 0; i < level*tabSize; i++)
|
||||
n += sink.write(' ');
|
||||
|
||||
return n;
|
||||
}
|
53
JsonGenerator/IndentedPrint.h
Normal file
53
JsonGenerator/IndentedPrint.h
Normal file
@ -0,0 +1,53 @@
|
||||
/*
|
||||
* Arduino JSON library
|
||||
* Benoit Blanchon 2014 - MIT License
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Print.h"
|
||||
|
||||
namespace ArduinoJson
|
||||
{
|
||||
namespace Generator
|
||||
{
|
||||
// Decorator on top of Print to allow indented output.
|
||||
// This class is used by JsonPrintable::prettyPrintTo() but can also be used
|
||||
// for your own purpose, like logging.
|
||||
class IndentedPrint : public Print
|
||||
{
|
||||
public:
|
||||
|
||||
IndentedPrint(Print& p)
|
||||
: sink(p)
|
||||
{
|
||||
level = 0;
|
||||
tabSize = 2;
|
||||
isNewLine = true;
|
||||
}
|
||||
|
||||
virtual size_t write(uint8_t);
|
||||
|
||||
// Adds one level of indentation
|
||||
void indent();
|
||||
|
||||
// Removes one level of indentation
|
||||
void unindent();
|
||||
|
||||
// Set the number of space printed for each level of indentation
|
||||
void setTabSize(uint8_t n);
|
||||
|
||||
private:
|
||||
Print& sink;
|
||||
uint8_t level : 4;
|
||||
uint8_t tabSize : 3;
|
||||
bool isNewLine : 1;
|
||||
|
||||
size_t writeTabs();
|
||||
|
||||
static const int MAX_LEVEL = 15; // because it's only 4 bits
|
||||
static const int MAX_TAB_SIZE = 7; // because it's only 3 bits
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -22,7 +22,7 @@ namespace ArduinoJson
|
||||
}
|
||||
|
||||
private:
|
||||
Internals::JsonValue items[N];
|
||||
JsonValue items[N];
|
||||
};
|
||||
}
|
||||
}
|
@ -6,6 +6,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "JsonPrintable.h"
|
||||
#include "JsonValue.h"
|
||||
|
||||
namespace ArduinoJson
|
||||
{
|
||||
@ -14,18 +15,40 @@ namespace ArduinoJson
|
||||
class JsonArrayBase : public JsonPrintable
|
||||
{
|
||||
public:
|
||||
JsonArrayBase(Internals::JsonValue* items, int capacity)
|
||||
JsonArrayBase(JsonValue* items, int capacity)
|
||||
: items(items), capacity(capacity), count(0)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void add(T value)
|
||||
void add(const Printable& value)
|
||||
{
|
||||
if (count >= capacity) return;
|
||||
addIfPossible<const Printable&>(value);
|
||||
}
|
||||
|
||||
items[count++].set(value);
|
||||
void add(bool value)
|
||||
{
|
||||
addIfPossible<bool>(value);
|
||||
}
|
||||
|
||||
void add(int value)
|
||||
{
|
||||
addIfPossible<long>(value);
|
||||
}
|
||||
|
||||
void add(long value)
|
||||
{
|
||||
addIfPossible<long>(value);
|
||||
}
|
||||
|
||||
void add(double value)
|
||||
{
|
||||
addIfPossible<double>(value);
|
||||
}
|
||||
|
||||
void add(const char* value)
|
||||
{
|
||||
addIfPossible<const char*>(value);
|
||||
}
|
||||
|
||||
template<int DIGITS>
|
||||
@ -33,7 +56,7 @@ namespace ArduinoJson
|
||||
{
|
||||
if (count >= capacity) return;
|
||||
|
||||
Internals::JsonValue& v = items[count++];
|
||||
JsonValue& v = items[count++];
|
||||
v.set<DIGITS>(value);
|
||||
}
|
||||
|
||||
@ -42,8 +65,15 @@ namespace ArduinoJson
|
||||
using JsonPrintable::printTo;
|
||||
|
||||
private:
|
||||
Internals::JsonValue* items;
|
||||
JsonValue* items;
|
||||
int capacity, count;
|
||||
|
||||
template<typename T>
|
||||
void addIfPossible(T value)
|
||||
{
|
||||
if (count < capacity)
|
||||
items[count++] = value;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
@ -12,6 +12,8 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="EscapedString.h" />
|
||||
<ClInclude Include="IndentedPrint.h" />
|
||||
<ClInclude Include="JsonPrettyPrint.h" />
|
||||
<ClInclude Include="JsonArray.h" />
|
||||
<ClInclude Include="JsonArrayBase.h" />
|
||||
<ClInclude Include="JsonObject.h" />
|
||||
@ -24,8 +26,11 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="EscapedString.cpp" />
|
||||
<ClCompile Include="IndentedPrint.cpp" />
|
||||
<ClCompile Include="JsonPrettyPrint.cpp" />
|
||||
<ClCompile Include="JsonArrayBase.cpp" />
|
||||
<ClCompile Include="JsonObjectBase.cpp" />
|
||||
<ClCompile Include="JsonPrintable.cpp" />
|
||||
<ClCompile Include="JsonValue.cpp" />
|
||||
<ClCompile Include="Print.cpp" />
|
||||
<ClCompile Include="StringBuilder.cpp" />
|
||||
|
@ -45,6 +45,12 @@
|
||||
<ClInclude Include="JsonObject.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="IndentedPrint.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="JsonPrettyPrint.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="EscapedString.cpp">
|
||||
@ -65,5 +71,14 @@
|
||||
<ClCompile Include="JsonObjectBase.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="IndentedPrint.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="JsonPrettyPrint.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="JsonPrintable.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
</Project>
|
@ -4,8 +4,12 @@
|
||||
*/
|
||||
|
||||
#include "JsonObjectBase.h"
|
||||
#include <string.h> // for strcmp
|
||||
|
||||
using namespace ArduinoJson::Generator;
|
||||
using namespace ArduinoJson::Internals;
|
||||
|
||||
JsonValue JsonObjectBase::nullValue;
|
||||
|
||||
size_t JsonObjectBase::printTo(Print& p) const
|
||||
{
|
||||
@ -18,7 +22,7 @@ size_t JsonObjectBase::printTo(Print& p) const
|
||||
const KeyValuePair* current = items;
|
||||
for (int i = count; i > 0; i--)
|
||||
{
|
||||
n += current->key.printTo(p);
|
||||
n += EscapedString::printTo(current->key, p);
|
||||
n += p.write(':');
|
||||
n += current->value.printTo(p);
|
||||
|
||||
@ -33,4 +37,56 @@ size_t JsonObjectBase::printTo(Print& p) const
|
||||
n += p.write('}');
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
JsonObjectBase::KeyValuePair* JsonObjectBase::getMatchingPair(JsonKey key) const
|
||||
{
|
||||
KeyValuePair* p = items;
|
||||
|
||||
for (int i = count; i > 0; --i)
|
||||
{
|
||||
if (!strcmp(p->key, key))
|
||||
return p;
|
||||
|
||||
p++;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
JsonValue& JsonObjectBase::operator[](JsonKey key)
|
||||
{
|
||||
KeyValuePair* match = getMatchingPair(key);
|
||||
|
||||
if (match)
|
||||
return match->value;
|
||||
|
||||
JsonValue* value;
|
||||
|
||||
if (count < capacity)
|
||||
{
|
||||
items[count].key = key;
|
||||
value = &items[count].value;
|
||||
count++;
|
||||
}
|
||||
else
|
||||
{
|
||||
value = &nullValue;
|
||||
}
|
||||
|
||||
value->reset();
|
||||
return *value;
|
||||
}
|
||||
|
||||
bool JsonObjectBase::containsKey(JsonKey key) const
|
||||
{
|
||||
return getMatchingPair(key) != 0;
|
||||
}
|
||||
|
||||
void JsonObjectBase::remove(JsonKey key)
|
||||
{
|
||||
KeyValuePair* match = getMatchingPair(key);
|
||||
if (match == 0) return;
|
||||
|
||||
*match = items[--count];
|
||||
}
|
@ -6,34 +6,32 @@
|
||||
#pragma once
|
||||
|
||||
#include "JsonPrintable.h"
|
||||
#include "JsonValue.h"
|
||||
#include "EscapedString.h"
|
||||
|
||||
namespace ArduinoJson
|
||||
{
|
||||
namespace Generator
|
||||
{
|
||||
typedef const char* JsonKey;
|
||||
|
||||
class JsonObjectBase : public JsonPrintable
|
||||
{
|
||||
public:
|
||||
JsonValue& operator[](JsonKey);
|
||||
bool containsKey(JsonKey) const;
|
||||
void remove(JsonKey key);
|
||||
|
||||
template<typename T>
|
||||
void add(const char* key, T value)
|
||||
void add(JsonKey key, T value)
|
||||
{
|
||||
if (count >= capacity) return;
|
||||
|
||||
items[count].key.set(key);
|
||||
items[count].value.set(value);
|
||||
count++;
|
||||
operator[](key) = value;
|
||||
}
|
||||
|
||||
template<int DIGITS>
|
||||
void add(const char* key, double value)
|
||||
void add(JsonKey key, double value)
|
||||
{
|
||||
if (count >= capacity) return;
|
||||
|
||||
items[count].key.set(key);
|
||||
items[count].value.set<DIGITS>(value);
|
||||
count++;
|
||||
operator[](key).set<DIGITS>(value);
|
||||
}
|
||||
|
||||
using JsonPrintable::printTo;
|
||||
@ -44,8 +42,8 @@ namespace ArduinoJson
|
||||
|
||||
struct KeyValuePair
|
||||
{
|
||||
Internals::EscapedString key;
|
||||
Internals::JsonValue value;
|
||||
JsonKey key;
|
||||
JsonValue value;
|
||||
};
|
||||
|
||||
JsonObjectBase(KeyValuePair* items, int capacity)
|
||||
@ -56,6 +54,9 @@ namespace ArduinoJson
|
||||
private:
|
||||
KeyValuePair* items;
|
||||
int capacity, count;
|
||||
static JsonValue nullValue;
|
||||
|
||||
KeyValuePair* getMatchingPair(JsonKey key) const;
|
||||
};
|
||||
}
|
||||
}
|
97
JsonGenerator/JsonPrettyPrint.cpp
Normal file
97
JsonGenerator/JsonPrettyPrint.cpp
Normal file
@ -0,0 +1,97 @@
|
||||
/*
|
||||
* Arduino JSON library
|
||||
* Benoit Blanchon 2014 - MIT License
|
||||
*/
|
||||
|
||||
#include "JsonPrettyPrint.h"
|
||||
|
||||
using namespace ArduinoJson::Generator;
|
||||
|
||||
size_t JsonPrettyPrint::write(uint8_t c)
|
||||
{
|
||||
size_t n = inString ? handleStringChar(c) : handleMarkupChar(c);
|
||||
previousChar = c;
|
||||
return n;
|
||||
}
|
||||
|
||||
inline size_t JsonPrettyPrint::handleStringChar(uint8_t c)
|
||||
{
|
||||
bool isQuote = c == '"' && previousChar != '\\';
|
||||
|
||||
if (isQuote) inString = false;
|
||||
|
||||
return sink.write(c);
|
||||
}
|
||||
|
||||
inline size_t JsonPrettyPrint::handleMarkupChar(uint8_t c)
|
||||
{
|
||||
switch (c)
|
||||
{
|
||||
case '{':
|
||||
case '[':
|
||||
return handleBlockOpen(c);
|
||||
|
||||
case '}':
|
||||
case ']':
|
||||
return handleBlockClose(c);
|
||||
|
||||
case ':':
|
||||
return handleColumn();
|
||||
|
||||
case ',':
|
||||
return handleComma();
|
||||
|
||||
case '"':
|
||||
return handleQuoteOpen();
|
||||
|
||||
default:
|
||||
return handleNormalChar(c);
|
||||
}
|
||||
}
|
||||
|
||||
inline size_t JsonPrettyPrint::handleBlockOpen(uint8_t c)
|
||||
{
|
||||
return indentIfNeeded() + sink.write(c);
|
||||
}
|
||||
|
||||
inline size_t JsonPrettyPrint::handleBlockClose(uint8_t c)
|
||||
{
|
||||
return unindentIfNeeded() + sink.write(c);
|
||||
}
|
||||
|
||||
inline size_t JsonPrettyPrint::handleColumn()
|
||||
{
|
||||
return sink.write(':') + sink.write(' ');
|
||||
}
|
||||
|
||||
inline size_t JsonPrettyPrint::handleComma()
|
||||
{
|
||||
return sink.write(',') + sink.println();
|
||||
}
|
||||
|
||||
inline size_t JsonPrettyPrint::handleQuoteOpen()
|
||||
{
|
||||
inString = true;
|
||||
return indentIfNeeded() + sink.write('"');
|
||||
}
|
||||
|
||||
inline size_t JsonPrettyPrint::handleNormalChar(uint8_t c)
|
||||
{
|
||||
return indentIfNeeded() + sink.write(c);
|
||||
}
|
||||
|
||||
size_t JsonPrettyPrint::indentIfNeeded()
|
||||
{
|
||||
if (!inEmptyBlock()) return 0;
|
||||
|
||||
sink.indent();
|
||||
return sink.println();
|
||||
}
|
||||
|
||||
size_t JsonPrettyPrint::unindentIfNeeded()
|
||||
{
|
||||
if (inEmptyBlock()) return 0;
|
||||
|
||||
sink.unindent();
|
||||
return sink.println();
|
||||
}
|
52
JsonGenerator/JsonPrettyPrint.h
Normal file
52
JsonGenerator/JsonPrettyPrint.h
Normal file
@ -0,0 +1,52 @@
|
||||
/*
|
||||
* Arduino JSON library
|
||||
* Benoit Blanchon 2014 - MIT License
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Print.h"
|
||||
#include "IndentedPrint.h"
|
||||
|
||||
namespace ArduinoJson
|
||||
{
|
||||
namespace Generator
|
||||
{
|
||||
// Converts a compact JSON string into an indented one.
|
||||
class JsonPrettyPrint : public Print
|
||||
{
|
||||
public:
|
||||
|
||||
JsonPrettyPrint(IndentedPrint& p)
|
||||
: sink(p)
|
||||
{
|
||||
previousChar = 0;
|
||||
inString = false;
|
||||
}
|
||||
|
||||
virtual size_t write(uint8_t);
|
||||
|
||||
private:
|
||||
uint8_t previousChar;
|
||||
IndentedPrint& sink;
|
||||
bool inString;
|
||||
|
||||
bool inEmptyBlock()
|
||||
{
|
||||
return previousChar == '{' || previousChar == '[';
|
||||
}
|
||||
|
||||
size_t handleStringChar(uint8_t);
|
||||
size_t handleMarkupChar(uint8_t);
|
||||
|
||||
size_t handleBlockClose(uint8_t);
|
||||
size_t handleBlockOpen(uint8_t);
|
||||
size_t handleColumn();
|
||||
size_t handleComma();
|
||||
size_t handleQuoteOpen();
|
||||
size_t handleNormalChar(uint8_t);
|
||||
size_t indentIfNeeded();
|
||||
size_t unindentIfNeeded();
|
||||
};
|
||||
}
|
||||
}
|
35
JsonGenerator/JsonPrintable.cpp
Normal file
35
JsonGenerator/JsonPrintable.cpp
Normal file
@ -0,0 +1,35 @@
|
||||
/*
|
||||
* Arduino JSON library
|
||||
* Benoit Blanchon 2014 - MIT License
|
||||
*/
|
||||
|
||||
#include "JsonPrintable.h"
|
||||
#include "JsonPrettyPrint.h"
|
||||
#include "StringBuilder.h"
|
||||
|
||||
using namespace ArduinoJson::Generator;
|
||||
using namespace ArduinoJson::Internals;
|
||||
|
||||
size_t JsonPrintable::printTo(char* buffer, size_t bufferSize) const
|
||||
{
|
||||
StringBuilder sb(buffer, bufferSize);
|
||||
return printTo(sb);
|
||||
}
|
||||
|
||||
size_t JsonPrintable::prettyPrintTo(char* buffer, size_t bufferSize) const
|
||||
{
|
||||
StringBuilder sb(buffer, bufferSize);
|
||||
return prettyPrintTo(sb);
|
||||
}
|
||||
|
||||
size_t JsonPrintable::prettyPrintTo(IndentedPrint& p) const
|
||||
{
|
||||
JsonPrettyPrint prettyPrint(p);
|
||||
return printTo(prettyPrint);
|
||||
}
|
||||
|
||||
size_t JsonPrintable::prettyPrintTo(Print& p) const
|
||||
{
|
||||
IndentedPrint indentedPrint(p);
|
||||
return prettyPrintTo(indentedPrint);
|
||||
}
|
@ -5,27 +5,36 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "JsonValue.h"
|
||||
#include "Print.h"
|
||||
#include "Printable.h"
|
||||
#include "IndentedPrint.h"
|
||||
|
||||
namespace ArduinoJson
|
||||
{
|
||||
namespace Generator
|
||||
{
|
||||
// Contains methods to generate a JSON string.
|
||||
// Implemented by both JsonObject and JsonArray
|
||||
class JsonPrintable : public Printable
|
||||
{
|
||||
public:
|
||||
|
||||
size_t printTo(char* buffer, size_t bufferSize)
|
||||
{
|
||||
using namespace Internals;
|
||||
|
||||
StringBuilder sb(buffer, bufferSize);
|
||||
return printTo(sb);
|
||||
}
|
||||
|
||||
// Generates the compact JSON string and sends it to a Print stream
|
||||
virtual size_t printTo(Print& p) const = 0;
|
||||
|
||||
// Generates the compact JSON string and writes it in a buffer
|
||||
size_t printTo(char* buffer, size_t bufferSize) const;
|
||||
|
||||
// Generates the indented JSON string and sends it to a Print stream
|
||||
size_t prettyPrintTo(Print& p) const;
|
||||
|
||||
// Generates the indented JSON string and sends it to a IndentedPrint stream
|
||||
// This overload allows a finer control of the output because you can customize
|
||||
// the IndentedPrint.
|
||||
size_t prettyPrintTo(IndentedPrint& p) const;
|
||||
|
||||
// Generates the indented JSON string and writes it in a buffer
|
||||
size_t prettyPrintTo(char* buffer, size_t bufferSize) const;
|
||||
};
|
||||
}
|
||||
}
|
@ -6,6 +6,7 @@
|
||||
#include "EscapedString.h"
|
||||
#include "JsonValue.h"
|
||||
|
||||
using namespace ArduinoJson::Generator;
|
||||
using namespace ArduinoJson::Internals;
|
||||
|
||||
size_t JsonValue::printBoolTo(const Content& c, Print& p)
|
||||
@ -28,5 +29,5 @@ size_t JsonValue::printPrintableTo(const Content& c, Print& p)
|
||||
|
||||
size_t JsonValue::printStringTo(const Content& c, Print& p)
|
||||
{
|
||||
return c.asString.printTo(p);
|
||||
return EscapedString::printTo(c.asString, p);
|
||||
}
|
@ -11,43 +11,43 @@
|
||||
|
||||
namespace ArduinoJson
|
||||
{
|
||||
namespace Internals
|
||||
namespace Generator
|
||||
{
|
||||
class JsonValue
|
||||
{
|
||||
public:
|
||||
|
||||
void set(bool value)
|
||||
void operator=(bool value)
|
||||
{
|
||||
printToImpl = &printBoolTo;
|
||||
content.asBool = value;
|
||||
}
|
||||
|
||||
void set(long value)
|
||||
void operator=(long value)
|
||||
{
|
||||
printToImpl = &printLongTo;
|
||||
content.asLong = value;
|
||||
}
|
||||
|
||||
void set(int value)
|
||||
void operator=(int value)
|
||||
{
|
||||
printToImpl = &printLongTo;
|
||||
content.asLong = value;
|
||||
}
|
||||
|
||||
void set(Printable& value)
|
||||
void operator=(const Printable& value)
|
||||
{
|
||||
printToImpl = &printPrintableTo;
|
||||
content.asPrintable = &value;
|
||||
}
|
||||
|
||||
void set(const char* value)
|
||||
void operator=(const char* value)
|
||||
{
|
||||
printToImpl = &printStringTo;
|
||||
content.asString.set(value);
|
||||
content.asString = value;
|
||||
}
|
||||
|
||||
void set(double value)
|
||||
void operator=(double value)
|
||||
{
|
||||
set<2>(value);
|
||||
}
|
||||
@ -55,29 +55,70 @@ namespace ArduinoJson
|
||||
template <int DIGITS>
|
||||
void set(double value)
|
||||
{
|
||||
printToImpl = &printDoubleTo<DIGITS>;
|
||||
printToImpl = &printDoubleTo < DIGITS > ;
|
||||
content.asDouble = value;
|
||||
}
|
||||
|
||||
operator bool()
|
||||
{
|
||||
return content.asBool;
|
||||
}
|
||||
|
||||
operator const char*()
|
||||
{
|
||||
return content.asString;
|
||||
}
|
||||
|
||||
operator double()
|
||||
{
|
||||
return content.asDouble;
|
||||
}
|
||||
|
||||
operator float()
|
||||
{
|
||||
return (float)content.asDouble;
|
||||
}
|
||||
|
||||
operator int()
|
||||
{
|
||||
return content.asLong;
|
||||
}
|
||||
|
||||
operator long()
|
||||
{
|
||||
return content.asLong;
|
||||
}
|
||||
|
||||
operator const Printable&()
|
||||
{
|
||||
return *content.asPrintable;
|
||||
}
|
||||
|
||||
size_t printTo(Print& p) const
|
||||
{
|
||||
// handmade polymorphism
|
||||
return printToImpl(content, p);
|
||||
}
|
||||
|
||||
void reset()
|
||||
{
|
||||
content.asDouble = 0;
|
||||
printToImpl = printStringTo;
|
||||
}
|
||||
|
||||
private:
|
||||
union Content
|
||||
{
|
||||
bool asBool;
|
||||
long asLong;
|
||||
Printable* asPrintable;
|
||||
EscapedString asString;
|
||||
double asDouble;
|
||||
bool asBool;
|
||||
double asDouble;
|
||||
long asLong;
|
||||
const Printable* asPrintable;
|
||||
const char* asString;
|
||||
};
|
||||
|
||||
Content content;
|
||||
|
||||
size_t(* printToImpl)(const Content&, Print&);
|
||||
size_t(*printToImpl)(const Content&, Print&);
|
||||
|
||||
static size_t printBoolTo(const Content&, Print&);
|
||||
static size_t printLongTo(const Content&, Print&);
|
||||
|
@ -32,4 +32,9 @@ size_t Print::print(long value)
|
||||
return print(tmp);
|
||||
}
|
||||
|
||||
size_t Print::println()
|
||||
{
|
||||
return write('\r') + write('\n');
|
||||
}
|
||||
|
||||
#endif
|
@ -19,6 +19,7 @@ public:
|
||||
size_t print(const char[]);
|
||||
size_t print(double, int = 2);
|
||||
size_t print(long);
|
||||
size_t println();
|
||||
};
|
||||
|
||||
#else
|
||||
|
@ -1,9 +1,9 @@
|
||||
Arduino JSON library - Generator
|
||||
================================
|
||||
|
||||
This library is a simple JSON encoder for embedded systems.
|
||||
*An elegant and efficient JSON encoder for embedded systems.*
|
||||
|
||||
It's design to be very lightweight, works without any allocation on the heap (no malloc) and supports nested objects.
|
||||
It's design to have the most intuitive API, the smallest footprint and works without any allocation on the heap (no malloc).
|
||||
|
||||
It has been written with Arduino in mind, but it isn't linked to Arduino libraries so you can use this library in any other C++ project.
|
||||
|
||||
@ -11,10 +11,11 @@ It has been written with Arduino in mind, but it isn't linked to Arduino librari
|
||||
Features
|
||||
--------
|
||||
|
||||
* Supports nested objects
|
||||
* Elegant API, very easy to use
|
||||
* Fixed memory allocation (no malloc)
|
||||
* Small footprint
|
||||
* Supports nested objects
|
||||
* Supports indented output
|
||||
* Implements Arduino's `Printable interface
|
||||
* MIT License
|
||||
|
||||
@ -23,13 +24,13 @@ Example
|
||||
-------
|
||||
|
||||
JsonArray<2> array;
|
||||
array.add<6>(48.756080);
|
||||
array.add<6>(2.302038);
|
||||
array.add<6>(48.756080); // <6> specifies the number of digits in the output
|
||||
array.add<6>(2.302038); // (the default is 2)
|
||||
|
||||
JsonObject<3> root;
|
||||
root.add("sensor", "gps");
|
||||
root.add("time", 1351824120);
|
||||
root.add("data", array);
|
||||
root["sensor"] = "gps";
|
||||
root["time"] = 1351824120;
|
||||
root["data"] = array;
|
||||
|
||||
Serial.print(root); // {"sensor":"gps","time":1351824120,"data":[48.756080,2.302038]}
|
||||
|
||||
@ -80,7 +81,7 @@ Then you can add strings, integer, booleans, etc:
|
||||
array.add(42);
|
||||
array.add(true);
|
||||
|
||||
There are two syntaxes for the floating point values:
|
||||
There are two syntaxes for floating point values:
|
||||
|
||||
array.add<4>(3.1415); // 4 digits: "3.1415"
|
||||
array.add(3.14); // 2 digits: "3.14"
|
||||
@ -101,6 +102,33 @@ or
|
||||
JsonObject<8> nestedObject;
|
||||
array.add(nestedObject);
|
||||
|
||||
> ##### CAUTION! Nested objects must be in memory
|
||||
> Calling `add()` makes the `JsonArray` store a pointer to the nested object.
|
||||
> This is designed to avoid memory duplication.
|
||||
> But it can only work if the object is in memory when `printTo()` is executed.
|
||||
> For instance, don't do this:
|
||||
>
|
||||
> void addNestedObject()
|
||||
> {
|
||||
> JsonObject<2> nestedObject;
|
||||
> // ...
|
||||
> array.add(nestedObject); // <- DON'T !!
|
||||
>
|
||||
> // array now contains a pointer to a local variable that will be
|
||||
> // discarded as soon as the function exits
|
||||
> }
|
||||
>
|
||||
> For the same reason, don't do this either:
|
||||
>
|
||||
> for( int i=0; i<100; i++)
|
||||
> {
|
||||
> JsonObject<2> nestedObject;
|
||||
> // ...
|
||||
> array.add(nestedObject); // <- DON'T !!
|
||||
> }
|
||||
> // array now contains 100 pointers to the same a local variable
|
||||
> // that is out of the scope anyway
|
||||
|
||||
#### JSON Object
|
||||
|
||||
You create a JSON object (ie hash-table/dictionary) with the following line:
|
||||
@ -111,24 +139,29 @@ Like with the array class, there is a template parameter that gives the capacity
|
||||
|
||||
Then you can add strings, integer, booleans, etc:
|
||||
|
||||
object.add("key1", "bazinga!");
|
||||
object.add("key2", 42);
|
||||
object.add("key3", true);
|
||||
object["key1"] = "bazinga!";
|
||||
object["key2"] = 42;
|
||||
object["key3"] = true;
|
||||
|
||||
As for the arrays, there are two syntaxes for the floating point values:
|
||||
|
||||
array.add<4>("key4", 3.1415); // 4 digits: "3.1415"
|
||||
array.add("key5", 3.14); // 2 digits: "3.14"
|
||||
object["key4"].set<4>(3.1415); // 4 digits "3.1415"
|
||||
object["key5"] = 3.1415; // default: 2 digits "3.14"
|
||||
|
||||
Finally you can add nested objects:
|
||||
|
||||
JsonArray<8> nestedArray;
|
||||
object.add("key6", nestedArray);
|
||||
object["key6"] = nestedArray;
|
||||
|
||||
or
|
||||
|
||||
JsonObject<8> nestedObject;
|
||||
object.add("key7", nestedObject);
|
||||
object["key7"] = nestedObject;
|
||||
|
||||
> ##### Other JsonObject functions
|
||||
> * `object.add(key, value)` is a synonym for `object[key] = value`
|
||||
> * `object.containsKey(key)` returns `true` is the `key` is present in `object`
|
||||
> * `object.remove(key)` removes the `value` associated with `key`
|
||||
|
||||
### 4. Get the JSON string
|
||||
|
||||
@ -145,6 +178,13 @@ Whether you have a `JsonArray` or a `JsonObject`, simply call `printTo()` with t
|
||||
char buffer[256];
|
||||
array.printTo(buffer, sizeof(buffer));
|
||||
|
||||
> ##### Want an indented output?
|
||||
> By default the generated JSON is as small as possible. It contains no extra space, nor line break.
|
||||
> But if you want an indented, more readable output, you can.
|
||||
> Simply call `prettyPrintTo` instead of `printTo()`:
|
||||
>
|
||||
> array.prettyPrintTo(buffer, sizeof(buffer));
|
||||
|
||||
#### Send to a stream
|
||||
|
||||
It's very likely that the generated JSON will end up in a stream like `Serial` or `EthernetClient `, so you can save some time and memory by doing this:
|
||||
@ -158,7 +198,7 @@ or
|
||||
> ##### About the Printable interface
|
||||
> `JsonArray` and `JsonObject` implement Arduino's `Printable` interface.
|
||||
> This is why you can call `Serial.print()` like in the example above.
|
||||
> You can do the same with any other implementation of `Print`: `HardwareSerial`, `SoftwareSerial`, `LiquidCrystal`, `EthernetClient`, `WiFiClient`...
|
||||
> You can do the same with any other implementation of `Print`: `HardwareSerial`, `SoftwareSerial`, `LiquidCrystal`, `EthernetClient`, `WiFiClient`, `Wire`...
|
||||
|
||||
|
||||
Memory usage
|
||||
@ -177,15 +217,20 @@ This table is for an 8-bit Arduino, types would be bigger on a 32-bit processor.
|
||||
Code size
|
||||
---------
|
||||
|
||||
The following values has been obtained with Arduino IDE 1.0.5, targeting an Arduino Duelmilanove with an ATmega 328.
|
||||
|
||||
### Minimum setup
|
||||
|
||||
| Function | Size |
|
||||
| ---------------------------- | ---- |
|
||||
| `JsonObjectBase::printTo()` | 222 |
|
||||
| `EscapedString::printTo()` | 202 |
|
||||
| `JsonArrayBase::printTo()` | 164 |
|
||||
| `Print::print(char const*)` | 146 |
|
||||
| `JsonValue::printStringTo()` | 6 |
|
||||
| Function | Size |
|
||||
| ----------------------------------- | ---- |
|
||||
| `JsonObjectBase::printTo()` | 234 |
|
||||
| `EscapedString::printTo()` | 196 |
|
||||
| `JsonArrayBase::printTo()` | 164 |
|
||||
| `Print::print(char const*)` | 146 |
|
||||
| `JsonObjectBase::operator[]` | 114 |
|
||||
| `JsonObjectBase::getMatchingPair()` | 72 |
|
||||
| `JsonValue::printPrintableTo()` | 40 |
|
||||
| `JsonValue::printStringTo()` | 12 |
|
||||
|
||||
### Additional space for integers
|
||||
|
||||
|
@ -16,8 +16,7 @@ namespace JsonGeneratorTests
|
||||
{
|
||||
char buffer[1024];
|
||||
size_t returnValue;
|
||||
EscapedString escapedString;
|
||||
|
||||
|
||||
public:
|
||||
|
||||
TEST_METHOD(Null)
|
||||
@ -83,9 +82,8 @@ namespace JsonGeneratorTests
|
||||
private:
|
||||
void whenInputIs(const char* input)
|
||||
{
|
||||
StringBuilder sb(buffer, sizeof(buffer));
|
||||
escapedString.set(input);
|
||||
returnValue = escapedString.printTo(sb);
|
||||
StringBuilder sb(buffer, sizeof(buffer));
|
||||
returnValue = EscapedString::printTo(input, sb);
|
||||
}
|
||||
|
||||
void outputMustBe(const char* expected)
|
||||
|
73
JsonGeneratorTests/Issue10.cpp
Normal file
73
JsonGeneratorTests/Issue10.cpp
Normal file
@ -0,0 +1,73 @@
|
||||
#include "CppUnitTest.h"
|
||||
#include "JsonArray.h"
|
||||
#include "JsonObject.h"
|
||||
|
||||
using namespace ArduinoJson::Generator;
|
||||
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
|
||||
|
||||
namespace JsonGeneratorTests
|
||||
{
|
||||
TEST_CLASS(Issue10)
|
||||
{
|
||||
struct Person {
|
||||
int id;
|
||||
char name[32];
|
||||
};
|
||||
|
||||
Person persons[2];
|
||||
|
||||
public:
|
||||
|
||||
TEST_METHOD_INITIALIZE(Initialize)
|
||||
{
|
||||
Person boss;
|
||||
boss.id = 1;
|
||||
strcpy(boss.name, "Jeff");
|
||||
Person employee;
|
||||
employee.id = 2;
|
||||
strcpy(employee.name, "John");
|
||||
persons[0] = boss;
|
||||
persons[1] = employee;
|
||||
}
|
||||
|
||||
TEST_METHOD(WrongWayToAddObjectInAnArray)
|
||||
{
|
||||
JsonArray<2> json;
|
||||
|
||||
for (int i = 0; i < 2; i++)
|
||||
{
|
||||
JsonObject<2> object;
|
||||
|
||||
object["id"] = persons[i].id;
|
||||
object["name"] = persons[i].name;
|
||||
|
||||
json.add(object); // <- Adding a reference to a temporary variable
|
||||
}
|
||||
|
||||
char buffer[256];
|
||||
json.printTo(buffer, sizeof(buffer));
|
||||
|
||||
// the same values are repeated, that's normal
|
||||
Assert::AreEqual("[{\"id\":2,\"name\":\"John\"},{\"id\":2,\"name\":\"John\"}]", buffer);
|
||||
}
|
||||
|
||||
TEST_METHOD(RightWayToAddObjectInAnArray)
|
||||
{
|
||||
JsonArray<2> json;
|
||||
JsonObject<2> object[2];
|
||||
|
||||
for (int i = 0; i < 2; i++)
|
||||
{
|
||||
object[i]["id"] = persons[i].id;
|
||||
object[i]["name"] = persons[i].name;
|
||||
|
||||
json.add(object[i]);
|
||||
}
|
||||
|
||||
char buffer[256];
|
||||
json.printTo(buffer, sizeof(buffer));
|
||||
|
||||
Assert::AreEqual("[{\"id\":1,\"name\":\"Jeff\"},{\"id\":2,\"name\":\"John\"}]", buffer);
|
||||
}
|
||||
};
|
||||
}
|
@ -14,7 +14,7 @@ namespace JsonGeneratorTests
|
||||
{
|
||||
TEST_CLASS(JsonArrayTests)
|
||||
{
|
||||
JsonArray<2> arr;
|
||||
JsonArray<2> array;
|
||||
char buffer[256];
|
||||
|
||||
public:
|
||||
@ -26,111 +26,117 @@ namespace JsonGeneratorTests
|
||||
|
||||
TEST_METHOD(Null)
|
||||
{
|
||||
add((char*)0);
|
||||
array.add((char*) 0);
|
||||
|
||||
outputMustBe("[null]");
|
||||
}
|
||||
|
||||
TEST_METHOD(OneString)
|
||||
{
|
||||
add("hello");
|
||||
array.add("hello");
|
||||
|
||||
outputMustBe("[\"hello\"]");
|
||||
}
|
||||
|
||||
TEST_METHOD(TwoStrings)
|
||||
{
|
||||
add("hello");
|
||||
add("world");
|
||||
array.add("hello");
|
||||
array.add("world");
|
||||
|
||||
outputMustBe("[\"hello\",\"world\"]");
|
||||
}
|
||||
|
||||
TEST_METHOD(OneStringOverCapacity)
|
||||
{
|
||||
add("hello");
|
||||
add("world");
|
||||
add("lost");
|
||||
array.add("hello");
|
||||
array.add("world");
|
||||
array.add("lost");
|
||||
|
||||
outputMustBe("[\"hello\",\"world\"]");
|
||||
}
|
||||
|
||||
TEST_METHOD(OneDoubleDefaultDigits)
|
||||
{
|
||||
add(3.14159265358979323846);
|
||||
array.add(3.14159265358979323846);
|
||||
outputMustBe("[3.14]");
|
||||
}
|
||||
|
||||
TEST_METHOD(OneDoubleFourDigits)
|
||||
{
|
||||
add<4>(3.14159265358979323846);
|
||||
array.add<4>(3.14159265358979323846);
|
||||
outputMustBe("[3.1416]");
|
||||
}
|
||||
|
||||
TEST_METHOD(OneInteger)
|
||||
{
|
||||
add(1);
|
||||
array.add(1);
|
||||
|
||||
outputMustBe("[1]");
|
||||
}
|
||||
|
||||
TEST_METHOD(TwoIntegers)
|
||||
{
|
||||
add(1);
|
||||
add(2);
|
||||
array.add(1);
|
||||
array.add(2);
|
||||
|
||||
outputMustBe("[1,2]");
|
||||
}
|
||||
|
||||
TEST_METHOD(OneIntegerOverCapacity)
|
||||
{
|
||||
add(1);
|
||||
add(2);
|
||||
add(3);
|
||||
array.add(1);
|
||||
array.add(2);
|
||||
array.add(3);
|
||||
|
||||
outputMustBe("[1,2]");
|
||||
}
|
||||
|
||||
TEST_METHOD(OneTrue)
|
||||
{
|
||||
add(true);
|
||||
array.add(true);
|
||||
|
||||
outputMustBe("[true]");
|
||||
}
|
||||
|
||||
TEST_METHOD(OneFalse)
|
||||
{
|
||||
add(false);
|
||||
array.add(false);
|
||||
|
||||
outputMustBe("[false]");
|
||||
}
|
||||
|
||||
TEST_METHOD(TwoBooleans)
|
||||
{
|
||||
add(false);
|
||||
add(true);
|
||||
array.add(false);
|
||||
array.add(true);
|
||||
|
||||
outputMustBe("[false,true]");
|
||||
}
|
||||
|
||||
TEST_METHOD(OneBooleanOverCapacity)
|
||||
{
|
||||
add(false);
|
||||
add(true);
|
||||
add(false);
|
||||
array.add(false);
|
||||
array.add(true);
|
||||
array.add(false);
|
||||
|
||||
outputMustBe("[false,true]");
|
||||
}
|
||||
|
||||
TEST_METHOD(OneEmptyNestedArray)
|
||||
{
|
||||
addNested(JsonArray<1>());
|
||||
JsonArray<1> nestedArray;
|
||||
|
||||
array.add(nestedArray);
|
||||
|
||||
outputMustBe("[[]]");
|
||||
}
|
||||
|
||||
TEST_METHOD(OneEmptyNestedHash)
|
||||
{
|
||||
addNested(JsonHashTable<1>());
|
||||
JsonObject<1> nestedObject;
|
||||
|
||||
array.add(nestedObject);
|
||||
|
||||
outputMustBe("[{}]");
|
||||
}
|
||||
|
||||
@ -139,33 +145,16 @@ namespace JsonGeneratorTests
|
||||
JsonArray<1> nestedArray;
|
||||
nestedArray.add(1);
|
||||
|
||||
addNested(nestedArray);
|
||||
array.add(nestedArray);
|
||||
|
||||
outputMustBe("[[1]]");
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
void addNested(Printable& value)
|
||||
{
|
||||
arr.add<Printable&>(value);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void add(T value)
|
||||
{
|
||||
arr.add(value);
|
||||
}
|
||||
|
||||
template<int DIGITS>
|
||||
void add(double value)
|
||||
{
|
||||
arr.add<DIGITS>(value);
|
||||
}
|
||||
|
||||
void outputMustBe(const char* expected)
|
||||
{
|
||||
size_t n = arr.printTo(buffer, sizeof(buffer));
|
||||
size_t n = array.printTo(buffer, sizeof(buffer));
|
||||
Assert::AreEqual(expected, buffer);
|
||||
Assert::AreEqual(strlen(expected), n);
|
||||
}
|
||||
|
@ -85,9 +85,15 @@
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="EscapedStringTests.cpp" />
|
||||
<ClCompile Include="PrettyPrint_Array_Tests.cpp" />
|
||||
<ClCompile Include="PrettyPrint_Object_Tests.cpp" />
|
||||
<ClCompile Include="PrettyPrint_String_Tests.cpp" />
|
||||
<ClCompile Include="Issue10.cpp" />
|
||||
<ClCompile Include="JsonArrayTests.cpp" />
|
||||
<ClCompile Include="JsonHashTableTests.cpp" />
|
||||
<ClCompile Include="JsonValueTests.cpp" />
|
||||
<ClCompile Include="JsonObject_Indexer_Tests.cpp" />
|
||||
<ClCompile Include="JsonObject_PrintTo_Tests.cpp" />
|
||||
<ClCompile Include="JsonValue_Cast_Tests.cpp" />
|
||||
<ClCompile Include="JsonValue_PrintTo_Tests.cpp" />
|
||||
<ClCompile Include="StringBuilderTests.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
|
@ -18,17 +18,35 @@
|
||||
<ClCompile Include="JsonArrayTests.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="JsonHashTableTests.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="JsonValueTests.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="StringBuilderTests.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="EscapedStringTests.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="JsonObject_PrintTo_Tests.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="JsonObject_Indexer_Tests.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="JsonValue_PrintTo_Tests.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="JsonValue_Cast_Tests.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Issue10.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="PrettyPrint_Array_Tests.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="PrettyPrint_Object_Tests.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="PrettyPrint_String_Tests.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
</Project>
|
@ -1,124 +0,0 @@
|
||||
/*
|
||||
* Arduino JSON library
|
||||
* Benoit Blanchon 2014 - MIT License
|
||||
*/
|
||||
|
||||
#include "CppUnitTest.h"
|
||||
#include "JsonArray.h"
|
||||
#include "JsonObject.h"
|
||||
|
||||
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
|
||||
using namespace ArduinoJson::Generator;
|
||||
|
||||
namespace JsonGeneratorTests
|
||||
{
|
||||
TEST_CLASS(JsonHashTableTests)
|
||||
{
|
||||
JsonHashTable<2> hash;
|
||||
char buffer[256];
|
||||
|
||||
public:
|
||||
|
||||
TEST_METHOD(Empty)
|
||||
{
|
||||
outputMustBe("{}");
|
||||
}
|
||||
|
||||
TEST_METHOD(OneString)
|
||||
{
|
||||
add("key", "value");
|
||||
outputMustBe("{\"key\":\"value\"}");
|
||||
}
|
||||
|
||||
TEST_METHOD(TwoStrings)
|
||||
{
|
||||
add("key1", "value1");
|
||||
add("key2", "value2");
|
||||
|
||||
outputMustBe("{\"key1\":\"value1\",\"key2\":\"value2\"}");
|
||||
}
|
||||
|
||||
TEST_METHOD(OneStringOverCapacity)
|
||||
{
|
||||
add("key1", "value1");
|
||||
add("key2", "value2");
|
||||
add("key3", "value3");
|
||||
|
||||
outputMustBe("{\"key1\":\"value1\",\"key2\":\"value2\"}");
|
||||
}
|
||||
|
||||
TEST_METHOD(OneInteger)
|
||||
{
|
||||
add("key", 1);
|
||||
outputMustBe("{\"key\":1}");
|
||||
}
|
||||
|
||||
TEST_METHOD(OneDoubleFourDigits)
|
||||
{
|
||||
add<4>("key", 3.14159265358979323846);
|
||||
outputMustBe("{\"key\":3.1416}");
|
||||
}
|
||||
|
||||
TEST_METHOD(OneDoubleDefaultDigits)
|
||||
{
|
||||
add("key", 3.14159265358979323846);
|
||||
outputMustBe("{\"key\":3.14}");
|
||||
}
|
||||
|
||||
TEST_METHOD(OneNull)
|
||||
{
|
||||
add("key", (char*) 0);
|
||||
outputMustBe("{\"key\":null}");
|
||||
}
|
||||
|
||||
TEST_METHOD(OneTrue)
|
||||
{
|
||||
add("key", true);
|
||||
outputMustBe("{\"key\":true}");
|
||||
}
|
||||
|
||||
TEST_METHOD(OneFalse)
|
||||
{
|
||||
add("key", false);
|
||||
outputMustBe("{\"key\":false}");
|
||||
}
|
||||
|
||||
TEST_METHOD(OneEmptyNestedArray)
|
||||
{
|
||||
addNested("key", JsonArray<1>());
|
||||
outputMustBe("{\"key\":[]}");
|
||||
}
|
||||
|
||||
TEST_METHOD(OneEmptyNestedHash)
|
||||
{
|
||||
addNested("key", JsonHashTable<1>());
|
||||
outputMustBe("{\"key\":{}}");
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
void addNested(const char* key, Printable& value)
|
||||
{
|
||||
hash.add<Printable&>(key, value);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void add(const char* key, T value)
|
||||
{
|
||||
hash.add(key, value);
|
||||
}
|
||||
|
||||
template<int DIGITS>
|
||||
void add(const char* key, double value)
|
||||
{
|
||||
hash.add<DIGITS>(key, value);
|
||||
}
|
||||
|
||||
void outputMustBe(const char* expected)
|
||||
{
|
||||
size_t actual = hash.printTo(buffer, sizeof(buffer));
|
||||
Assert::AreEqual(expected, buffer);
|
||||
Assert::AreEqual(strlen(expected), actual);
|
||||
}
|
||||
};
|
||||
}
|
73
JsonGeneratorTests/JsonObject_Indexer_Tests.cpp
Normal file
73
JsonGeneratorTests/JsonObject_Indexer_Tests.cpp
Normal file
@ -0,0 +1,73 @@
|
||||
/*
|
||||
* Arduino JSON library
|
||||
* Benoit Blanchon 2014 - MIT License
|
||||
*/
|
||||
|
||||
#include "CppUnitTest.h"
|
||||
#include "JsonArray.h"
|
||||
#include "JsonObject.h"
|
||||
|
||||
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
|
||||
using namespace ArduinoJson::Generator;
|
||||
|
||||
namespace JsonGeneratorTests
|
||||
{
|
||||
TEST_CLASS(JsonObject_Indexer_Tests)
|
||||
{
|
||||
JsonObject<2> object;
|
||||
|
||||
public:
|
||||
|
||||
TEST_METHOD(Empty)
|
||||
{
|
||||
mustNotContain("key");
|
||||
}
|
||||
|
||||
TEST_METHOD(TwoStrings)
|
||||
{
|
||||
object["key1"] = "value1";
|
||||
object["key2"] = "value2";
|
||||
|
||||
mustContain("key1", "value1");
|
||||
mustContain("key2", "value2");
|
||||
}
|
||||
|
||||
TEST_METHOD(RemoveFirst)
|
||||
{
|
||||
object["key1"] = "value1";
|
||||
object["key2"] = "value2";
|
||||
object.remove("key1");
|
||||
|
||||
mustNotContain("key1");
|
||||
mustContain("key2", "value2");
|
||||
}
|
||||
|
||||
TEST_METHOD(RemoveLast)
|
||||
{
|
||||
object["key1"] = "value1";
|
||||
object["key2"] = "value2";
|
||||
object.remove("key2");
|
||||
|
||||
mustContain("key1", "value1");
|
||||
mustNotContain("key2");
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
void mustContain(const char* key, const char* expected)
|
||||
{
|
||||
Assert::IsTrue(object.containsKey(key));
|
||||
|
||||
const char* actual = object[key];
|
||||
Assert::AreEqual(expected, actual);
|
||||
}
|
||||
|
||||
void mustNotContain(const char* key)
|
||||
{
|
||||
Assert::IsFalse(object.containsKey(key));
|
||||
|
||||
const char* actual = object[key];
|
||||
Assert::IsNull(actual);
|
||||
}
|
||||
};
|
||||
}
|
152
JsonGeneratorTests/JsonObject_PrintTo_Tests.cpp
Normal file
152
JsonGeneratorTests/JsonObject_PrintTo_Tests.cpp
Normal file
@ -0,0 +1,152 @@
|
||||
/*
|
||||
* Arduino JSON library
|
||||
* Benoit Blanchon 2014 - MIT License
|
||||
*/
|
||||
|
||||
#include "CppUnitTest.h"
|
||||
#include "JsonArray.h"
|
||||
#include "JsonObject.h"
|
||||
|
||||
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
|
||||
using namespace ArduinoJson::Generator;
|
||||
|
||||
namespace JsonGeneratorTests
|
||||
{
|
||||
TEST_CLASS(JsonObject_PrintTo_Tests)
|
||||
{
|
||||
JsonObject<2> object;
|
||||
|
||||
public:
|
||||
|
||||
TEST_METHOD(Empty)
|
||||
{
|
||||
outputMustBe("{}");
|
||||
}
|
||||
|
||||
TEST_METHOD(OneString)
|
||||
{
|
||||
object["key"] = "value";
|
||||
|
||||
outputMustBe("{\"key\":\"value\"}");
|
||||
}
|
||||
|
||||
TEST_METHOD(TwoStrings)
|
||||
{
|
||||
object["key1"] = "value1";
|
||||
object["key2"] = "value2";
|
||||
|
||||
outputMustBe("{\"key1\":\"value1\",\"key2\":\"value2\"}");
|
||||
}
|
||||
|
||||
TEST_METHOD(RemoveFirst)
|
||||
{
|
||||
object["key1"] = "value1";
|
||||
object["key2"] = "value2";
|
||||
object.remove("key1");
|
||||
|
||||
outputMustBe("{\"key2\":\"value2\"}");
|
||||
}
|
||||
|
||||
TEST_METHOD(RemoveLast)
|
||||
{
|
||||
object["key1"] = "value1";
|
||||
object["key2"] = "value2";
|
||||
object.remove("key2");
|
||||
|
||||
outputMustBe("{\"key1\":\"value1\"}");
|
||||
}
|
||||
|
||||
TEST_METHOD(RemoveUnexistingKey)
|
||||
{
|
||||
object["key1"] = "value1";
|
||||
object["key2"] = "value2";
|
||||
object.remove("key3");
|
||||
|
||||
outputMustBe("{\"key1\":\"value1\",\"key2\":\"value2\"}");
|
||||
}
|
||||
|
||||
TEST_METHOD(ReplaceExistingKey)
|
||||
{
|
||||
object["key"] = "value1";
|
||||
object["key"] = "value2";
|
||||
|
||||
outputMustBe("{\"key\":\"value2\"}");
|
||||
}
|
||||
|
||||
TEST_METHOD(OneStringOverCapacity)
|
||||
{
|
||||
object["key1"] = "value1";
|
||||
object["key2"] = "value2";
|
||||
object["key3"] = "value3";
|
||||
|
||||
outputMustBe("{\"key1\":\"value1\",\"key2\":\"value2\"}");
|
||||
}
|
||||
|
||||
TEST_METHOD(OneInteger)
|
||||
{
|
||||
object["key"] = 1;
|
||||
outputMustBe("{\"key\":1}");
|
||||
}
|
||||
|
||||
TEST_METHOD(OneDoubleFourDigits)
|
||||
{
|
||||
object["key"].set<4>(3.14159265358979323846);
|
||||
outputMustBe("{\"key\":3.1416}");
|
||||
}
|
||||
|
||||
TEST_METHOD(OneDoubleDefaultDigits)
|
||||
{
|
||||
object["key"] = 3.14159265358979323846;
|
||||
outputMustBe("{\"key\":3.14}");
|
||||
}
|
||||
|
||||
TEST_METHOD(OneNull)
|
||||
{
|
||||
object["key"] = (char*) 0;
|
||||
outputMustBe("{\"key\":null}");
|
||||
}
|
||||
|
||||
TEST_METHOD(OneTrue)
|
||||
{
|
||||
object["key"] = true;
|
||||
outputMustBe("{\"key\":true}");
|
||||
}
|
||||
|
||||
TEST_METHOD(OneFalse)
|
||||
{
|
||||
object["key"] = false;
|
||||
outputMustBe("{\"key\":false}");
|
||||
}
|
||||
|
||||
TEST_METHOD(OneEmptyNestedArray)
|
||||
{
|
||||
auto nestedArray = JsonArray<1>();
|
||||
|
||||
object["key"] = nestedArray;
|
||||
|
||||
outputMustBe("{\"key\":[]}");
|
||||
}
|
||||
|
||||
TEST_METHOD(OneEmptyNestedObject)
|
||||
{
|
||||
auto nestedObject = JsonObject<1>();
|
||||
|
||||
object["key"] = nestedObject;
|
||||
|
||||
outputMustBe("{\"key\":{}}");
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
void outputMustBe(const char* expected)
|
||||
{
|
||||
char buffer[256];
|
||||
size_t result;
|
||||
|
||||
result = object.printTo(buffer, sizeof(buffer));
|
||||
|
||||
Assert::AreEqual(strlen(expected), result);
|
||||
Assert::AreEqual(expected, buffer);
|
||||
}
|
||||
};
|
||||
}
|
78
JsonGeneratorTests/JsonValue_Cast_Tests.cpp
Normal file
78
JsonGeneratorTests/JsonValue_Cast_Tests.cpp
Normal file
@ -0,0 +1,78 @@
|
||||
/*
|
||||
* Arduino JSON library
|
||||
* Benoit Blanchon 2014 - MIT License
|
||||
*/
|
||||
|
||||
#include "CppUnitTest.h"
|
||||
#include "StringBuilder.h"
|
||||
#include "JsonValue.h"
|
||||
#include "JsonArray.h"
|
||||
|
||||
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
|
||||
using namespace ArduinoJson::Generator;
|
||||
using namespace ArduinoJson::Internals;
|
||||
|
||||
namespace JsonGeneratorTests
|
||||
{
|
||||
TEST_CLASS(JsonValue_Cast_Tests)
|
||||
{
|
||||
JsonValue value;
|
||||
|
||||
public:
|
||||
|
||||
TEST_METHOD(Bool)
|
||||
{
|
||||
setValueAndCheckCast(true);
|
||||
setValueAndCheckCast(false);
|
||||
}
|
||||
|
||||
TEST_METHOD(Double)
|
||||
{
|
||||
setValueAndCheckCast(3.14156);
|
||||
}
|
||||
|
||||
TEST_METHOD(Float)
|
||||
{
|
||||
setValueAndCheckCast(3.14f);
|
||||
}
|
||||
|
||||
TEST_METHOD(Integer)
|
||||
{
|
||||
setValueAndCheckCast(42);
|
||||
}
|
||||
|
||||
TEST_METHOD(Long)
|
||||
{
|
||||
setValueAndCheckCast(42L);
|
||||
}
|
||||
|
||||
TEST_METHOD(Array)
|
||||
{
|
||||
JsonArray<2> array;
|
||||
setValueAndCheckCast(array);
|
||||
}
|
||||
|
||||
TEST_METHOD(String)
|
||||
{
|
||||
setValueAndCheckCast("hello");
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
template<typename T>
|
||||
void setValueAndCheckCast(T expected)
|
||||
{
|
||||
value = expected;
|
||||
T actual = value;
|
||||
Assert::AreEqual(expected, actual);
|
||||
}
|
||||
|
||||
template<int N>
|
||||
void setValueAndCheckCast(JsonArray<N>& expected)
|
||||
{
|
||||
value = expected;
|
||||
const Printable& actual = value;
|
||||
Assert::AreEqual((void*) &expected, (void*) &actual);
|
||||
}
|
||||
};
|
||||
}
|
@ -8,11 +8,12 @@
|
||||
#include "JsonValue.h"
|
||||
|
||||
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
|
||||
using namespace ArduinoJson::Generator;
|
||||
using namespace ArduinoJson::Internals;
|
||||
|
||||
namespace JsonGeneratorTests
|
||||
{
|
||||
TEST_CLASS(JsonValueTests)
|
||||
TEST_CLASS(JsonValue_PrintTo_Tests)
|
||||
{
|
||||
char buffer[1024];
|
||||
size_t returnValue;
|
||||
@ -21,62 +22,62 @@ namespace JsonGeneratorTests
|
||||
|
||||
TEST_METHOD(String)
|
||||
{
|
||||
whenInputIs("hello");
|
||||
setValueTo("hello");
|
||||
outputMustBe("\"hello\"");
|
||||
}
|
||||
|
||||
TEST_METHOD(Float)
|
||||
{
|
||||
whenInputIs(3.1415f);
|
||||
setValueTo(3.1415f);
|
||||
outputMustBe("3.14");
|
||||
}
|
||||
|
||||
TEST_METHOD(DoubleZeroDigits)
|
||||
{
|
||||
whenInputIs<0>(3.14159265358979323846);
|
||||
setValueTo<0>(3.14159265358979323846);
|
||||
outputMustBe("3");
|
||||
}
|
||||
|
||||
TEST_METHOD(DoubleOneDigit)
|
||||
{
|
||||
whenInputIs<1>(3.14159265358979323846);
|
||||
setValueTo<1>(3.14159265358979323846);
|
||||
outputMustBe("3.1");
|
||||
}
|
||||
|
||||
TEST_METHOD(DoubleTwoDigits)
|
||||
{
|
||||
whenInputIs<2>(3.14159265358979323846);
|
||||
setValueTo<2>(3.14159265358979323846);
|
||||
outputMustBe("3.14");
|
||||
}
|
||||
|
||||
TEST_METHOD(Integer)
|
||||
{
|
||||
whenInputIs(314);
|
||||
setValueTo(314);
|
||||
outputMustBe("314");
|
||||
}
|
||||
|
||||
TEST_METHOD(Char)
|
||||
{
|
||||
whenInputIs('A');
|
||||
setValueTo('A');
|
||||
outputMustBe("65");
|
||||
}
|
||||
|
||||
TEST_METHOD(Short)
|
||||
{
|
||||
whenInputIs((short)314);
|
||||
setValueTo((short)314);
|
||||
outputMustBe("314");
|
||||
}
|
||||
|
||||
TEST_METHOD(Long)
|
||||
{
|
||||
whenInputIs(314159265L);
|
||||
setValueTo(314159265L);
|
||||
outputMustBe("314159265");
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
template<int DIGITS>
|
||||
void whenInputIs(double value)
|
||||
void setValueTo(double value)
|
||||
{
|
||||
StringBuilder sb(buffer, sizeof(buffer));
|
||||
JsonValue jsonValue;
|
||||
@ -85,11 +86,11 @@ namespace JsonGeneratorTests
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void whenInputIs(T value)
|
||||
void setValueTo(T value)
|
||||
{
|
||||
StringBuilder sb(buffer, sizeof(buffer));
|
||||
JsonValue jsonValue;
|
||||
jsonValue.set(value);
|
||||
jsonValue = value;
|
||||
returnValue = jsonValue.printTo(sb);
|
||||
}
|
||||
|
91
JsonGeneratorTests/PrettyPrint_Array_Tests.cpp
Normal file
91
JsonGeneratorTests/PrettyPrint_Array_Tests.cpp
Normal file
@ -0,0 +1,91 @@
|
||||
/*
|
||||
* Arduino JSON library
|
||||
* Benoit Blanchon 2014 - MIT License
|
||||
*/
|
||||
|
||||
#include "CppUnitTest.h"
|
||||
#include "JsonPrettyPrint.h"
|
||||
#include "StringBuilder.h"
|
||||
|
||||
using namespace ArduinoJson::Internals;
|
||||
using namespace ArduinoJson::Generator;
|
||||
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
|
||||
|
||||
namespace JsonGeneratorTests
|
||||
{
|
||||
TEST_CLASS(PrettyPrint_Array_Tests)
|
||||
{
|
||||
char buffer[1024];
|
||||
size_t returnValue;
|
||||
|
||||
public:
|
||||
|
||||
TEST_METHOD(EmptyArray)
|
||||
{
|
||||
whenInputIs("[]");
|
||||
outputMustBe("[]");
|
||||
}
|
||||
|
||||
TEST_METHOD(OneElement)
|
||||
{
|
||||
whenInputIs("[1]");
|
||||
outputMustBe(
|
||||
"[\r\n"
|
||||
" 1\r\n"
|
||||
"]");
|
||||
}
|
||||
|
||||
TEST_METHOD(TwoElements)
|
||||
{
|
||||
whenInputIs("[1,2]");
|
||||
outputMustBe(
|
||||
"[\r\n"
|
||||
" 1,\r\n"
|
||||
" 2\r\n"
|
||||
"]");
|
||||
}
|
||||
|
||||
TEST_METHOD(EmptyNestedArrays)
|
||||
{
|
||||
whenInputIs("[[],[]]");
|
||||
outputMustBe(
|
||||
"[\r\n"
|
||||
" [],\r\n"
|
||||
" []\r\n"
|
||||
"]");
|
||||
}
|
||||
|
||||
TEST_METHOD(NestedArrays)
|
||||
{
|
||||
whenInputIs("[[1,2],[3,4]]");
|
||||
outputMustBe(
|
||||
"[\r\n"
|
||||
" [\r\n"
|
||||
" 1,\r\n"
|
||||
" 2\r\n"
|
||||
" ],\r\n"
|
||||
" [\r\n"
|
||||
" 3,\r\n"
|
||||
" 4\r\n"
|
||||
" ]\r\n"
|
||||
"]");
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
void whenInputIs(const char input[])
|
||||
{
|
||||
StringBuilder sb(buffer, sizeof(buffer));
|
||||
IndentedPrint indentedPrint(sb);
|
||||
JsonPrettyPrint decorator(indentedPrint);
|
||||
|
||||
returnValue = decorator.print(input);
|
||||
}
|
||||
|
||||
void outputMustBe(const char* expected)
|
||||
{
|
||||
Assert::AreEqual(expected, buffer);
|
||||
Assert::AreEqual(strlen(expected), returnValue);
|
||||
}
|
||||
};
|
||||
}
|
89
JsonGeneratorTests/PrettyPrint_Object_Tests.cpp
Normal file
89
JsonGeneratorTests/PrettyPrint_Object_Tests.cpp
Normal file
@ -0,0 +1,89 @@
|
||||
/*
|
||||
* Arduino JSON library
|
||||
* Benoit Blanchon 2014 - MIT License
|
||||
*/
|
||||
|
||||
#include "CppUnitTest.h"
|
||||
#include "JsonPrettyPrint.h"
|
||||
#include "StringBuilder.h"
|
||||
|
||||
using namespace ArduinoJson::Internals;
|
||||
using namespace ArduinoJson::Generator;
|
||||
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
|
||||
|
||||
namespace JsonGeneratorTests
|
||||
{
|
||||
TEST_CLASS(PrettyPrint_Object_Tests)
|
||||
{
|
||||
char buffer[1024];
|
||||
size_t returnValue;
|
||||
|
||||
public:
|
||||
|
||||
TEST_METHOD(EmptyObject)
|
||||
{
|
||||
whenInputIs("{}");
|
||||
outputMustBe("{}");
|
||||
}
|
||||
|
||||
TEST_METHOD(OneMember)
|
||||
{
|
||||
whenInputIs("{\"key\":\"value\"}");
|
||||
outputMustBe(
|
||||
"{\r\n"
|
||||
" \"key\": \"value\"\r\n"
|
||||
"}");
|
||||
}
|
||||
|
||||
TEST_METHOD(TwoMembers)
|
||||
{
|
||||
whenInputIs("{\"key1\":\"value1\",\"key2\":\"value2\"}");
|
||||
outputMustBe(
|
||||
"{\r\n"
|
||||
" \"key1\": \"value1\",\r\n"
|
||||
" \"key2\": \"value2\"\r\n"
|
||||
"}");
|
||||
}
|
||||
|
||||
TEST_METHOD(EmptyNestedObjects)
|
||||
{
|
||||
whenInputIs("{\"key1\":{},\"key2\":{}}");
|
||||
outputMustBe(
|
||||
"{\r\n"
|
||||
" \"key1\": {},\r\n"
|
||||
" \"key2\": {}\r\n"
|
||||
"}");
|
||||
}
|
||||
|
||||
TEST_METHOD(NestedObjects)
|
||||
{
|
||||
whenInputIs("{\"key1\":{\"a\":1},\"key2\":{\"b\":2}}");
|
||||
outputMustBe(
|
||||
"{\r\n"
|
||||
" \"key1\": {\r\n"
|
||||
" \"a\": 1\r\n"
|
||||
" },\r\n"
|
||||
" \"key2\": {\r\n"
|
||||
" \"b\": 2\r\n"
|
||||
" }\r\n"
|
||||
"}");
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
void whenInputIs(const char input[])
|
||||
{
|
||||
StringBuilder sb(buffer, sizeof(buffer));
|
||||
IndentedPrint indentedPrint(sb);
|
||||
JsonPrettyPrint decorator(indentedPrint);
|
||||
|
||||
returnValue = decorator.print(input);
|
||||
}
|
||||
|
||||
void outputMustBe(const char* expected)
|
||||
{
|
||||
Assert::AreEqual(expected, buffer);
|
||||
Assert::AreEqual(strlen(expected), returnValue);
|
||||
}
|
||||
};
|
||||
}
|
76
JsonGeneratorTests/PrettyPrint_String_Tests.cpp
Normal file
76
JsonGeneratorTests/PrettyPrint_String_Tests.cpp
Normal file
@ -0,0 +1,76 @@
|
||||
/*
|
||||
* Arduino JSON library
|
||||
* Benoit Blanchon 2014 - MIT License
|
||||
*/
|
||||
|
||||
#include "CppUnitTest.h"
|
||||
#include "JsonPrettyPrint.h"
|
||||
#include "StringBuilder.h"
|
||||
|
||||
using namespace ArduinoJson::Internals;
|
||||
using namespace ArduinoJson::Generator;
|
||||
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
|
||||
|
||||
namespace JsonGeneratorTests
|
||||
{
|
||||
TEST_CLASS(PrettyPrint_String_Tests)
|
||||
{
|
||||
char buffer[1024];
|
||||
size_t returnValue;
|
||||
|
||||
public:
|
||||
|
||||
TEST_METHOD(EmptyString)
|
||||
{
|
||||
whenInputIs("");
|
||||
outputMustBe("");
|
||||
}
|
||||
|
||||
TEST_METHOD(TrickyCharacters)
|
||||
{
|
||||
whenInputIs ("\":\\\"',\"");
|
||||
outputMustBe("\":\\\"',\"");
|
||||
}
|
||||
|
||||
TEST_METHOD(OpeningCurlyBrace)
|
||||
{
|
||||
whenInputIs ("\"{\"");
|
||||
outputMustBe("\"{\"");
|
||||
}
|
||||
|
||||
TEST_METHOD(OpeningSquareBrace)
|
||||
{
|
||||
whenInputIs("\"[\"");
|
||||
outputMustBe("\"[\"");
|
||||
}
|
||||
|
||||
TEST_METHOD(ClosingCurlyBrace)
|
||||
{
|
||||
whenInputIs("\"}\"");
|
||||
outputMustBe("\"}\"");
|
||||
}
|
||||
|
||||
TEST_METHOD(ClosingSquareBrace)
|
||||
{
|
||||
whenInputIs("\"]\"");
|
||||
outputMustBe("\"]\"");
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
void whenInputIs(const char input[])
|
||||
{
|
||||
StringBuilder sb(buffer, sizeof(buffer));
|
||||
IndentedPrint indentedPrint(sb);
|
||||
JsonPrettyPrint decorator(indentedPrint);
|
||||
|
||||
returnValue = decorator.print(input);
|
||||
}
|
||||
|
||||
void outputMustBe(const char* expected)
|
||||
{
|
||||
Assert::AreEqual(expected, buffer);
|
||||
Assert::AreEqual(strlen(expected), returnValue);
|
||||
}
|
||||
};
|
||||
}
|
@ -3,7 +3,7 @@ Arduino JSON library
|
||||
|
||||
*An elegant and efficient JSON library for embedded systems.*
|
||||
|
||||
It's design to be very lightweight, works without any allocation on the heap (no malloc).
|
||||
It's design to have the most intuitive API, the smallest footprint and works without any allocation on the heap (no malloc).
|
||||
|
||||
It has been written with Arduino in mind, but it isn't linked to Arduino libraries so you can use this library in any other C++ project.
|
||||
|
||||
@ -22,9 +22,9 @@ Feature comparison
|
||||
|
||||
| Library | Memory allocation | Nested objects | Parser size | Encoder size |
|
||||
| ------------ | ----------------- | -------------- | ----------- | ------------- |
|
||||
| Arduino JSON | static | yes | 2642 Bytes | 628 bytes |
|
||||
| Arduino JSON | static | yes | 2642 Bytes | 862 bytes |
|
||||
| json-arduino | dynamic | no | 3348 (+27%) | not supported |
|
||||
| aJson | dynamic | yes | 5088 (+93%) | 4678 (+640%) |
|
||||
| aJson | dynamic | yes | 5088 (+93%) | 4678 (+540%) |
|
||||
|
||||
"Parser size" was measured with a program parsing `{"sensor":"outdoor","value":25.6}`.
|
||||
For each library, I wrote a program that extracts a string and a float. I subtracted the size of a program doing the same without any JSON parsing involved. [Source files are here](https://gist.github.com/bblanchon/e8ba914a7109f3642c0f).
|
||||
|
43
examples/IndentedPrintExample/IndentedPrintExample.ino
Normal file
43
examples/IndentedPrintExample/IndentedPrintExample.ino
Normal file
@ -0,0 +1,43 @@
|
||||
/*
|
||||
* Arduino JSON library - IndentedPrint example
|
||||
* Benoit Blanchon 2014 - MIT License
|
||||
*/
|
||||
|
||||
#include <JsonGenerator.h>
|
||||
|
||||
using namespace ArduinoJson::Generator;
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(9600);
|
||||
|
||||
JsonObject<1> json;
|
||||
json["key"] = "value";
|
||||
|
||||
IndentedPrint serial(Serial);
|
||||
serial.setTabSize(4);
|
||||
|
||||
serial.println("This is at indentation 0");
|
||||
serial.indent();
|
||||
serial.println("This is at indentation 1");
|
||||
serial.println("This is also at indentation 1");
|
||||
serial.indent();
|
||||
serial.println("This is at indentation 2");
|
||||
|
||||
serial.println("You can print JSON here, as usual:");
|
||||
serial.println(json);
|
||||
serial.println();
|
||||
|
||||
serial.println("But you can also prettyPrint JSON here:");
|
||||
json.prettyPrintTo(serial);
|
||||
serial.println();
|
||||
|
||||
serial.unindent();
|
||||
serial.unindent();
|
||||
serial.println("This is back at indentation 0");
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
|
||||
}
|
@ -16,11 +16,14 @@ void setup()
|
||||
array.add<6>(2.302038); // if not specified, 2 digits are printed
|
||||
|
||||
JsonObject<3> root;
|
||||
root.add("sensor", "gps");
|
||||
root.add("time", 1351824120);
|
||||
root.add("data", array);
|
||||
root["sensor"] = "gps";
|
||||
root["time"] = 1351824120;
|
||||
root["data"] = array;
|
||||
|
||||
Serial.print(root); // {"sensor":"gps","time":1351824120,"data":[48.756080,2.302038]}
|
||||
|
||||
Serial.println();
|
||||
root.prettyPrintTo(Serial); // same string indented
|
||||
}
|
||||
|
||||
void loop()
|
||||
|
Reference in New Issue
Block a user